Java Code Examples for java.nio.ByteOrder#LITTLE_ENDIAN

The following examples show how to use java.nio.ByteOrder#LITTLE_ENDIAN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IcoRoundtripTest.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
@Test
public void test32bitMask() throws Exception {
    final int foreground = 0xFFF000E0;
    final int background = 0xFF102030;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final BinaryOutputStream bos = new BinaryOutputStream(baos,
            ByteOrder.LITTLE_ENDIAN);
    // For 32 bit RGBA, the AND mask can be missing:
    final byte[] bitmap = new GeneratorFor32BitBitmaps().generate32bitRGBABitmap(
            foreground, background, 0, false);
    writeICONDIR(bos, 0, 1, 1);
    writeICONDIRENTRY(bos, 16, 16, 0, 0, 1, 32, 40 + bitmap.length);
    writeBITMAPINFOHEADER(bos, 16, 2 * 16, 1, 32, 0, 0, 0);
    bos.write(bitmap);
    bos.flush();
    writeAndReadImageData("16x16x32-no-mask", baos.toByteArray(),
            foreground, background);
}
 
Example 2
Source File: BinaryFrame.java    From jmbe with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Constructs a binary frame from the byte array with length set to the
 * number of bits contained in the byte array
 */
public static BinaryFrame fromBytes(byte[] data, ByteOrder byteOrder)
{
    if(byteOrder == ByteOrder.LITTLE_ENDIAN)
    {
        BinaryFrame message = new BinaryFrame(data.length * 8);

        for(int x = 0; x < data.length; x++)
        {
            message.setByte(x * 8, data[x]);
        }

        return message;
    }
    else
    {
        BinaryFrame frame = new BinaryFrame( data.length * 8 );
        BitSet bitSet = BitSet.valueOf(data);
        frame.xor(bitSet);
        return frame;
    }
}
 
Example 3
Source File: LibVlcUtil.java    From VLC-Simple-Player-Android with MIT License 6 votes vote down vote up
private static boolean readHeader(RandomAccessFile in, ElfData elf) throws IOException {
    // http://www.sco.com/developers/gabi/1998-04-29/ch4.eheader.html
    byte[] bytes = new byte[ELF_HEADER_SIZE];
    in.readFully(bytes);
    if (bytes[0] != 127 ||
            bytes[1] != 'E' ||
            bytes[2] != 'L' ||
            bytes[3] != 'F' ||
            bytes[4] != 1) { // ELFCLASS32, Only 32bit header is supported
        return false;
    }

    elf.order = bytes[5] == 1
            ? ByteOrder.LITTLE_ENDIAN // ELFDATA2LSB
            : ByteOrder.BIG_ENDIAN;   // ELFDATA2MSB

    // wrap bytes in a ByteBuffer to force endianess
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(elf.order);

    elf.e_machine = buffer.getShort(18);    /* Architecture */
    elf.e_shoff = buffer.getInt(32);        /* Section header table file offset */
    elf.e_shnum = buffer.getShort(48);      /* Section header table entry count */
    return true;
}
 
Example 4
Source File: FloatDeclarationTest.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Test the equals
 */
@Test
public void equalsTest() {
    FloatDeclaration a = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 0);
    FloatDeclaration b = new FloatDeclaration(8, 24, ByteOrder.LITTLE_ENDIAN, 0);
    FloatDeclaration c = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
    FloatDeclaration d = new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 0);
    FloatDeclaration e = new FloatDeclaration(24, 24, ByteOrder.BIG_ENDIAN, 0);
    FloatDeclaration f = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 0);
    assertNotEquals(a, null);
    assertNotEquals(a, new Object());
    assertNotEquals(a, b);
    assertNotEquals(a, c);
    assertNotEquals(a, d);
    assertNotEquals(b, a);
    assertNotEquals(c, a);
    assertNotEquals(d, a);
    assertNotEquals(e, a);
    assertNotEquals(a, e);

    assertEquals(a, f);
    assertEquals(f, a);
    assertEquals(a, a);
}
 
Example 5
Source File: NBTInputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long readLong() throws IOException {
    if (network) {
        return VarInt.readVarLong(this.stream);
    }
    long l = this.stream.readLong();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        l = Long.reverseBytes(l);
    }
    return l;
}
 
Example 6
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeShort(int v) throws IOException {
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        v = Integer.reverseBytes(v) >> 16;
    }
    this.stream.writeShort(v);
}
 
Example 7
Source File: AbstractPerfDataBufferPrologue.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the byte order.
 *
 * @return int - the byte order of the instrumentation buffer
 */
public ByteOrder getByteOrder() {
    // byte order field is byte order independent
    byteBuffer.position(PERFDATA_PROLOG_BYTEORDER_OFFSET);

    byte byte_order = byteBuffer.get();

    if (byte_order == PERFDATA_BIG_ENDIAN) {
        return ByteOrder.BIG_ENDIAN;
    } else {
        return ByteOrder.LITTLE_ENDIAN;
    }
}
 
Example 8
Source File: SwappedByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public SwappedByteBuf(ByteBuf buf) {
    if (buf == null) {
        throw new NullPointerException("buf");
    }
    this.buf = buf;
    if (buf.order() == ByteOrder.BIG_ENDIAN) {
        order = ByteOrder.LITTLE_ENDIAN;
    } else {
        order = ByteOrder.BIG_ENDIAN;
    }
}
 
Example 9
Source File: AbstractPerfDataBufferPrologue.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the byte order for the given ByteBuffer.
 *
 * @return int - the byte order of the instrumentation buffer
 */
public static ByteOrder getByteOrder(ByteBuffer bb) {
    // save buffer state
    int position = bb.position();

    bb.position(PERFDATA_PROLOG_BYTEORDER_OFFSET);
    ByteOrder order = (bb.get() == PERFDATA_BIG_ENDIAN)
                      ? ByteOrder.BIG_ENDIAN
                      : ByteOrder.LITTLE_ENDIAN;

    // restore buffer state.
    bb.position(position);
    return order;
}
 
Example 10
Source File: NBTInputStream.java    From Nemisys with GNU General Public License v3.0 5 votes vote down vote up
@Override
public char readChar() throws IOException {
    char c = this.stream.readChar();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        c = Character.reverseBytes(c);
    }
    return c;
}
 
Example 11
Source File: IcoRoundtripTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Override
       public byte[] generateBitmap(final int foreground, final int background,
final int paletteSize)
               throws IOException, ImageWriteException {
           try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
                   final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
               // Palette
               for (int i = 0; i < paletteSize; i++) {
                   bos.write4Bytes(0);
               }
               // Image
               for (int y = 15; y >= 0; y--) {
                   for (int x = 0; x < 16; x++) {
                       if (IMAGE[y][x] == 1) {
                           bos.write2Bytes((0x1f & (foreground >> 3)) | ((0x1f & (foreground >> 11)) << 5)
                                   | ((0x1f & (foreground >> 19)) << 10));
                       } else {
                           bos.write2Bytes((0x1f & (background >> 3)) | ((0x1f & (background >> 11)) << 5)
                                   | ((0x1f & (background >> 19)) << 10));
                       }
                   }
               }
               // Mask
               for (int y = IMAGE.length - 1; y >= 0; y--) {
                   bos.write(0);
                   bos.write(0);
                   // Pad to 4 bytes:
                   bos.write(0);
                   bos.write(0);
               }
               bos.flush();
               return byteArrayStream.toByteArray();
           }
       }
 
Example 12
Source File: NBTOutputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeInt(int v) throws IOException {
    if (network) {
        VarInt.writeVarInt(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Integer.reverseBytes(v);
        }
        this.stream.writeInt(v);
    }
}
 
Example 13
Source File: ByteConversions.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
private static void toBytes(final float value, final ByteOrder byteOrder, final byte[] result, final int offset) {
    final int bits = Float.floatToRawIntBits(value);
    if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
        result[offset + 0] = (byte) (0xff & (bits >> 0));
        result[offset + 1] = (byte) (0xff & (bits >> 8));
        result[offset + 2] = (byte) (0xff & (bits >> 16));
        result[offset + 3] = (byte) (0xff & (bits >> 24));
    } else {
        result[offset + 3] = (byte) (0xff & (bits >> 0));
        result[offset + 2] = (byte) (0xff & (bits >> 8));
        result[offset + 1] = (byte) (0xff & (bits >> 16));
        result[offset + 0] = (byte) (0xff & (bits >> 24));
    }
}
 
Example 14
Source File: NBTOutputStream.java    From BukkitPE with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeLong(long v) throws IOException {
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        v = Long.reverseBytes(v);
    }
    this.stream.writeLong(v);
}
 
Example 15
Source File: MotifDnDConstants.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * DragBSI.h:
 *
 * typedef struct {
 *    BYTE          byte_order;
 *    BYTE          protocol_version;
 *    CARD16        num_target_lists B16;
 *    CARD32        heap_offset B32;
 * } xmMotifTargetsPropertyRec;
 */
private static long[][] getTargetListTable(long motifWindow)
  throws XException {

    WindowPropertyGetter wpg = new WindowPropertyGetter(motifWindow,
                                                        XA_MOTIF_DRAG_TARGETS,
                                                        0, 100000L,
                                                        false,
                                                        XA_MOTIF_DRAG_TARGETS.getAtom());
    try {
        int status = wpg.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());

        if (status != XConstants.Success
            || wpg.getActualType() != XA_MOTIF_DRAG_TARGETS.getAtom()
            || wpg.getData() == 0) {

            return null;
        }

        long data = wpg.getData();

        if (unsafe.getByte(data + 1) != MOTIF_DND_PROTOCOL_VERSION) {
            return null;
        }

        boolean swapNeeded = unsafe.getByte(data + 0) != getByteOrderByte();

        short numTargetLists = unsafe.getShort(data + 2);

        if (swapNeeded) {
            numTargetLists = Swapper.swap(numTargetLists);
        }

        long[][] table = new long[numTargetLists][];
        ByteOrder byteOrder = ByteOrder.nativeOrder();
        if (swapNeeded) {
            byteOrder = (byteOrder == ByteOrder.LITTLE_ENDIAN) ?
                ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
        }

        long bufptr = data + 8;
        for (short i = 0; i < numTargetLists; i++) {
            short numTargets = unsafe.getShort(bufptr);
            bufptr += 2;
            if (swapNeeded) {
                numTargets = Swapper.swap(numTargets);
            }

            table[i] = new long[numTargets];

            for (short j = 0; j < numTargets; j++) {
                // NOTE: cannot use Unsafe.getInt(), since it crashes on
                // Solaris/Sparc if the address is not a multiple of 4.
                int target = 0;
                if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
                    for (int idx = 0; idx < 4; idx++) {
                        target |= (unsafe.getByte(bufptr + idx) << 8*idx)
                            & (0xFF << 8*idx);
                    }
                } else {
                    for (int idx = 0; idx < 4; idx++) {
                        target |= (unsafe.getByte(bufptr + idx) << 8*(3-idx))
                            & (0xFF << 8*(3-idx));
                    }
                }
                // NOTE: don't need to swap, since we read it in the proper
                // order already.
                table[i][j] = target;
                bufptr += 4;
            }
        }
        return table;
    } finally {
        wpg.dispose();
    }
}
 
Example 16
Source File: PcapOldFile.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Constructor of the PcapOldFile class where the parent is PcapFile class.
 *
 * @param filePath
 *            The path to the pcap file.
 *
 * @throws BadPcapFileException
 *             Thrown if the Pcap File is not valid.
 * @throws IOException
 *             Thrown if there is an IO error while reading the file.
 */
public PcapOldFile(Path filePath) throws BadPcapFileException, IOException {
    // Instantiate the parent class
    super(filePath);

    ByteOrder byteOrder;

    TreeMap<Long, Long> fileIndex = getFileIndex();
    // Parse the global header.
    // Read the magic number (4 bytes) from the input stream
    // and determine the mode (big endian or little endian)
    ByteBuffer globalHeader = ByteBuffer.allocate(PcapFileValues.GLOBAL_HEADER_SIZE);
    getFileChannel().read(globalHeader);
    globalHeader.flip();
    int magicNumber = globalHeader.getInt();

    switch (magicNumber) {
    case PcapFileValues.MAGIC_BIG_ENDIAN_MICRO: // file is big endian
        byteOrder = ByteOrder.BIG_ENDIAN;
        fTimestampPrecision = PcapTimestampScale.MICROSECOND;
        break;
    case PcapFileValues.MAGIC_LITTLE_ENDIAN_MICRO: // file is little endian
        byteOrder = ByteOrder.LITTLE_ENDIAN;
        fTimestampPrecision = PcapTimestampScale.MICROSECOND;
        break;
    case PcapFileValues.MAGIC_BIG_ENDIAN_NANO: // file is big endian
        byteOrder = ByteOrder.BIG_ENDIAN;
        fTimestampPrecision = PcapTimestampScale.NANOSECOND;
        break;
    case PcapFileValues.MAGIC_LITTLE_ENDIAN_NANO: // file is little endian
        byteOrder = ByteOrder.LITTLE_ENDIAN;
        fTimestampPrecision = PcapTimestampScale.NANOSECOND;
        break;
    default:
        this.close();
        throw new BadPcapFileException(String.format("%08x", magicNumber) + " is not a known magic number."); //$NON-NLS-1$ //$NON-NLS-2$
    }

    // Put the rest of the buffer in file endian.
    globalHeader.order(byteOrder);

    // Initialization of global header fields.
    int fMajorVersion = ConversionHelper.unsignedShortToInt(globalHeader.getShort());
    int fMinorVersion = ConversionHelper.unsignedShortToInt(globalHeader.getShort());
    fTimeAccuracy = ConversionHelper.unsignedIntToLong(globalHeader.getInt());
    fTimeZoneCorrection = ConversionHelper.unsignedIntToLong(globalHeader.getInt());
    fSnapshotLength = ConversionHelper.unsignedIntToLong(globalHeader.getInt());
    fDataLinkType = ConversionHelper.unsignedIntToLong(globalHeader.getInt());

    fileIndex.put(getCurrentRank(), getFileChannel().position());
    // Data initialization
    init(byteOrder, fMajorVersion, fMinorVersion);
}
 
Example 17
Source File: MethodHandleImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public ByteOrder memoryAddressByteOrder(VarHandle handle) {
    return checkMemAccessHandle(handle).be ?
            ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
}
 
Example 18
Source File: OptimizedNullArrayDumpBytesColumnBinaryMaker.java    From yosegi with Apache License 2.0 4 votes vote down vote up
@Override
public void loadInMemoryStorage(
    final ColumnBinary columnBinary ,
    final IMemoryAllocator allocator ) throws IOException {
  ICompressor compressor = FindCompressor.get( columnBinary.compressorClassName );
  byte[] binary = compressor.decompress(
      columnBinary.binary ,
      columnBinary.binaryStart ,
      columnBinary.binaryLength );
  ByteBuffer wrapBuffer = ByteBuffer.wrap( binary , 0 , binary.length );
  ByteOrder order = wrapBuffer.get() == (byte)0 ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  int startIndex = wrapBuffer.getInt();
  int minLength = wrapBuffer.getInt();
  int maxLength = wrapBuffer.getInt();
  int nullLength = wrapBuffer.getInt();
  int lengthBinaryLength = wrapBuffer.getInt();

  boolean[] isNullArray =
      NullBinaryEncoder.toIsNullArray( binary , META_LENGTH , nullLength );

  allocator.setValueCount( startIndex + isNullArray.length );

  IReadSupporter lengthReader;
  if ( minLength == maxLength ) {
    lengthReader = NumberToBinaryUtils.getFixedIntConverter( minLength );
  } else {
    NumberToBinaryUtils.IIntConverter lengthConverter =
        NumberToBinaryUtils.getIntConverter( minLength , maxLength );
    lengthReader = lengthConverter.toReadSupporter(
        binary ,
        META_LENGTH + nullLength ,
        lengthBinaryLength );
  }

  int currentStart = META_LENGTH + nullLength + lengthBinaryLength;
  for ( int i = 0 ; i < startIndex ; i++ ) {
    allocator.setNull( i );
  }
  for ( int i = 0 ; i < isNullArray.length ; i++ ) {
    if ( isNullArray[i]  ) {
      allocator.setNull( i + startIndex );
    } else {
      int currentLength = lengthReader.getInt();
      allocator.setBytes( i + startIndex , binary , currentStart , currentLength );
      currentStart += currentLength;
    }
  }
}
 
Example 19
Source File: ZipUtils.java    From titan-hotfix with Apache License 2.0 4 votes vote down vote up
private static void assertByteOrderLittleEndian(ByteBuffer buffer) {
    if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
        throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
    }
}
 
Example 20
Source File: PcxImageParser.java    From commons-imaging with Apache License 2.0 4 votes vote down vote up
public PcxImageParser() {
    super.setByteOrder(ByteOrder.LITTLE_ENDIAN);
}