Java Code Examples for java.nio.ByteOrder#BIG_ENDIAN

The following examples show how to use java.nio.ByteOrder#BIG_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: NumberToBinaryUtils.java    From yosegi with Apache License 2.0 6 votes vote down vote up
@Override
public IWriteSupporter toWriteSuppoter( final int rows , final byte[] buffer ,
    final int start , final int length ) throws IOException {
  int byteLength = Byte.BYTES * rows;
  int intLength = Integer.BYTES * rows;

  ByteOrder order = ByteOrder.nativeOrder();
  byte byteOrderByte = order == ByteOrder.BIG_ENDIAN ? (byte)0 : (byte)1;

  buffer[start] = HEADER_5;
  buffer[start + 1] = byteOrderByte;

  int byteStart = start + HEADER_SIZE;
  int intStart = byteStart + byteLength;

  IWriteSupporter byteSupporter = ByteBufferSupporterFactory.createWriteSupporter(
      buffer , byteStart , byteLength , order
  );
  IWriteSupporter intSupporter = ByteBufferSupporterFactory.createWriteSupporter(
      buffer , intStart , intLength , order
  );
  return new WriteSupporter5( byteSupporter , intSupporter );
}
 
Example 2
Source File: HotSpotResolvedObjectTypeImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static ResolvedJavaField findFieldWithOffset(long offset, JavaKind expectedEntryKind, ResolvedJavaField[] declaredFields) {
    for (ResolvedJavaField field : declaredFields) {
        HotSpotResolvedJavaField resolvedField = (HotSpotResolvedJavaField) field;
        long resolvedFieldOffset = resolvedField.offset();
        // @formatter:off
        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN  &&
                        expectedEntryKind.isPrimitive() &&
                        !expectedEntryKind.equals(JavaKind.Void) &&
                        resolvedField.getJavaKind().isPrimitive()) {
            resolvedFieldOffset +=
                            resolvedField.getJavaKind().getByteCount() -
                            Math.min(resolvedField.getJavaKind().getByteCount(), 4 + expectedEntryKind.getByteCount());
        }
        if (resolvedFieldOffset == offset) {
            return field;
        }
        // @formatter:on
    }
    return null;
}
 
Example 3
Source File: NumberToBinaryUtils.java    From yosegi with Apache License 2.0 6 votes vote down vote up
@Override
public IWriteSupporter toWriteSuppoter( final int rows , final byte[] buffer ,
    final int start , final int length ) throws IOException {
  int shortLength = Short.BYTES * rows;

  ByteOrder order = ByteOrder.nativeOrder();
  byte byteOrderByte = order == ByteOrder.BIG_ENDIAN ? (byte)0 : (byte)1;

  buffer[start] = HEADER_2;
  buffer[start + 1] = byteOrderByte;

  int shortStart = start + HEADER_SIZE;

  return new WriteSupporter2(
      ByteBufferSupporterFactory.createWriteSupporter(
      buffer , shortStart , shortLength , order )
  );
}
 
Example 4
Source File: FixedPoint.java    From jhdf with MIT License 6 votes vote down vote up
public FixedPoint(ByteBuffer bb) {
	super(bb);

	if (classBits.get(0)) {
		order = ByteOrder.BIG_ENDIAN;
	} else {
		order = ByteOrder.LITTLE_ENDIAN;
	}

	lowPadding = classBits.get(1);
	highPadding = classBits.get(2);
	signed = classBits.get(3);

	bitOffset = bb.getShort();
	bitPrecision = bb.getShort();
}
 
Example 5
Source File: ImageOutputStreamImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void writeLong(long v) throws IOException {
    if (byteOrder == ByteOrder.BIG_ENDIAN) {
        byteBuf[0] = (byte)(v >>> 56);
        byteBuf[1] = (byte)(v >>> 48);
        byteBuf[2] = (byte)(v >>> 40);
        byteBuf[3] = (byte)(v >>> 32);
        byteBuf[4] = (byte)(v >>> 24);
        byteBuf[5] = (byte)(v >>> 16);
        byteBuf[6] = (byte)(v >>>  8);
        byteBuf[7] = (byte)(v >>>  0);
    } else {
        byteBuf[0] = (byte)(v >>>  0);
        byteBuf[1] = (byte)(v >>>  8);
        byteBuf[2] = (byte)(v >>> 16);
        byteBuf[3] = (byte)(v >>> 24);
        byteBuf[4] = (byte)(v >>> 32);
        byteBuf[5] = (byte)(v >>> 40);
        byteBuf[6] = (byte)(v >>> 48);
        byteBuf[7] = (byte)(v >>> 56);
    }
    // REMIND: Once 6277756 is fixed, we should do a bulk write of all 8
    // bytes here as we do in writeShort() and writeInt() for even better
    // performance.  For now, two bulk writes of 4 bytes each is still
    // faster than 8 individual write() calls (see 6347575 for details).
    write(byteBuf, 0, 4);
    write(byteBuf, 4, 4);
}
 
Example 6
Source File: CompositeChannelBuffer.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
public int getUnsignedMedium(int index) {
    int componentId = componentId(index);
    if (index + 3 <= indices[componentId + 1]) {
        return components[componentId].getUnsignedMedium(index - indices[componentId]);
    } else if (order() == ByteOrder.BIG_ENDIAN) {
        return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff;
    } else {
        return getShort(index) & 0xFFFF | (getByte(index + 2) & 0xFF) << 16;
    }
}
 
Example 7
Source File: ImageInputStreamImpl.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
public short readShort() throws IOException {
    if (read(byteBuf, 0, 2) != 2) {
        throw new EOFException();
    }

    if (byteOrder == ByteOrder.BIG_ENDIAN) {
        return (short)
            (((byteBuf[0] & 0xff) << 8) | ((byteBuf[1] & 0xff) << 0));
    } else {
        return (short)
            (((byteBuf[1] & 0xff) << 8) | ((byteBuf[0] & 0xff) << 0));
    }
}
 
Example 8
Source File: CompositeByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void _setShort(int index, int value) {
    Component c = findComponent(index);
    if (index + 2 <= c.endOffset) {
        c.buf.setShort(index - c.offset, value);
    } else if (order() == ByteOrder.BIG_ENDIAN) {
        _setByte(index, (byte) (value >>> 8));
        _setByte(index + 1, (byte) value);
    } else {
        _setByte(index, (byte) value);
        _setByte(index + 1, (byte) (value >>> 8));
    }
}
 
Example 9
Source File: Memory.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public static short peekShort(byte[] src, int offset, ByteOrder order) {
    if (order == ByteOrder.BIG_ENDIAN) {
        return (short) ((src[offset] << 8) | (src[offset + 1] & 0xff));
    } else {
        return (short) ((src[offset + 1] << 8) | (src[offset] & 0xff));
    }
}
 
Example 10
Source File: VariableLenStickPackageHelper.java    From Android-Serialport with MIT License 5 votes vote down vote up
private int getLen(byte[] src, ByteOrder order) {
    int re = 0;
    if (order == ByteOrder.BIG_ENDIAN) {
        for (byte b : src) {
            re = (re << 8) | (b & 0xff);
        }
    } else {
        for (int i = src.length - 1; i >= 0; i--) {
            re = (re << 8) | (src[i] & 0xff);
        }
    }
    return re;
}
 
Example 11
Source File: ByteOrderUtil.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public ByteOrder forValue(String value){

	if(("BIG_ENDIAN").equalsIgnoreCase(value) || ("BE").equalsIgnoreCase(value)){
		return ByteOrder.BIG_ENDIAN;
	} else

	if(("LITTLE_ENDIAN").equalsIgnoreCase(value) || ("LE").equalsIgnoreCase(value)){
		return ByteOrder.LITTLE_ENDIAN;
	} else

	{
		throw new IllegalArgumentException(value);
	}
}
 
Example 12
Source File: CompositeByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void _setInt(int index, int value) {
    Component c = findComponent(index);
    if (index + 4 <= c.endOffset) {
        c.buf.setInt(index - c.offset, value);
    } else if (order() == ByteOrder.BIG_ENDIAN) {
        _setShort(index, (short) (value >>> 16));
        _setShort(index + 2, (short) value);
    } else {
        _setShort(index, (short) value);
        _setShort(index + 2, (short) (value >>> 16));
    }
}
 
Example 13
Source File: CompositeByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected int _getIntLE(int index) {
    Component c = findComponent(index);
    if (index + 4 <= c.endOffset) {
        return c.buf.getIntLE(index - c.offset);
    } else if (order() == ByteOrder.BIG_ENDIAN) {
        return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16;
    } else {
        return (_getShortLE(index) & 0xffff) << 16 | _getShortLE(index + 2) & 0xffff;
    }
}
 
Example 14
Source File: PCA9685.java    From diozero with MIT License 5 votes vote down vote up
public PCA9685(int controller, int address, int pwmFrequency) throws RuntimeIOException {
	super(DEVICE_NAME + "-" + controller + "-" + address);
	
	i2cDevice = new I2CDevice(controller, address, I2CConstants.ADDR_SIZE_7, I2CConstants.DEFAULT_CLOCK_FREQUENCY,
			ByteOrder.BIG_ENDIAN);
	boardPinInfo = new PCA9685BoardPinInfo();
	
	reset();
	
	setPwmFreq(pwmFrequency);
}
 
Example 15
Source File: ImageOutputStreamImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void writeShort(int v) throws IOException {
    if (byteOrder == ByteOrder.BIG_ENDIAN) {
        byteBuf[0] = (byte)(v >>> 8);
        byteBuf[1] = (byte)(v >>> 0);
    } else {
        byteBuf[0] = (byte)(v >>> 0);
        byteBuf[1] = (byte)(v >>> 8);
    }
    write(byteBuf, 0, 2);
}
 
Example 16
Source File: ImageInputStreamImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public short readShort() throws IOException {
    if (read(byteBuf, 0, 2) != 2) {
        throw new EOFException();
    }

    if (byteOrder == ByteOrder.BIG_ENDIAN) {
        return (short)
            (((byteBuf[0] & 0xff) << 8) | ((byteBuf[1] & 0xff) << 0));
    } else {
        return (short)
            (((byteBuf[1] & 0xff) << 8) | ((byteBuf[0] & 0xff) << 0));
    }
}
 
Example 17
Source File: UnsafeOptimizeDumpLongColumnBinaryMaker.java    From yosegi with Apache License 2.0 4 votes vote down vote up
@Override
public ColumnBinary toBinary(
    final ColumnBinaryMakerConfig commonConfig ,
    final ColumnBinaryMakerCustomConfigNode currentConfigNode ,
    final CompressResultNode compressResultNode ,
    final IColumn column ) throws IOException {
  ColumnBinaryMakerConfig currentConfig = commonConfig;
  if ( currentConfigNode != null ) {
    currentConfig = currentConfigNode.getCurrentConfig();
  }
  long[] valueArray = new long[column.size()];
  byte[] isNullArray = new byte[column.size()];

  Long min = Long.MAX_VALUE;
  Long max = Long.MIN_VALUE;
  int rowCount = 0;
  boolean hasNull = false;
  for ( int i = 0 ; i < column.size() ; i++ ) {
    ICell cell = column.get(i);
    PrimitiveObject primitiveObj = null;
    if ( cell.getType() == ColumnType.NULL ) {
      hasNull = true;
      isNullArray[i] = 1;
    } else {
      PrimitiveCell stringCell = (PrimitiveCell) cell;
      primitiveObj = stringCell.getRow();
      valueArray[rowCount] = primitiveObj.getLong();
      if ( 0 < min.compareTo( valueArray[rowCount] ) ) {
        min = Long.valueOf( valueArray[rowCount] );
      }
      if ( max.compareTo( valueArray[rowCount] ) < 0 ) {
        max = Long.valueOf( valueArray[rowCount] );
      }
      rowCount++;
    }
  }

  if ( ! hasNull && min.equals( max ) ) {
    return ConstantColumnBinaryMaker.createColumnBinary(
        createConstObjectFromNum( column.getColumnType() , min ) ,
        column.getColumnName() ,
        column.size() );
  }

  IBinaryMaker binaryMaker = chooseBinaryMaker( min.longValue() , max.longValue() );
  ByteOrder order = ByteOrder.nativeOrder();

  int nullBinaryLength = 0;
  if ( hasNull ) {
    nullBinaryLength = isNullArray.length;
  }
  int valueLength = binaryMaker.calcBinarySize( rowCount );

  byte[] binaryRaw = new byte[ nullBinaryLength + valueLength ];
  if ( hasNull ) {
    ByteBuffer compressBinaryBuffer = ByteBuffer.wrap( binaryRaw , 0 , nullBinaryLength );
    compressBinaryBuffer.put( isNullArray );
  }
  binaryMaker.create(
      valueArray , binaryRaw , nullBinaryLength , valueLength , order , rowCount );

  CompressResult compressResult = compressResultNode.getCompressResult(
      this.getClass().getName() ,
      "c0"  ,
      currentConfig.compressionPolicy ,
      currentConfig.allowedRatio );
  byte[] compressBinary = currentConfig.compressorClass.compress(
      binaryRaw , 0 , binaryRaw.length , compressResult );

  byte[] binary =
      new byte[ Long.BYTES * 2 + Byte.BYTES * 2 + Integer.BYTES + compressBinary.length ];

  byte byteOrderByte = order == ByteOrder.BIG_ENDIAN ? (byte)0 : (byte)1;

  ByteBuffer wrapBuffer = ByteBuffer.wrap( binary , 0 , binary.length );
  wrapBuffer.putLong( min );
  wrapBuffer.putLong( max );
  wrapBuffer.put( hasNull ? (byte)1 : (byte)0 );
  wrapBuffer.put( byteOrderByte );
  wrapBuffer.putInt( rowCount );
  wrapBuffer.put( compressBinary );

  return new ColumnBinary(
      this.getClass().getName() ,
      currentConfig.compressorClass.getClass().getName() ,
      column.getColumnName() ,
      column.getColumnType() ,
      column.size() ,
      binary.length ,
      getLogicalSize( rowCount , column.getColumnType() ) ,
      -1 ,
      binary ,
      0 ,
      binary.length ,
      null );
}
 
Example 18
Source File: OptimizedNullArrayFloatColumnBinaryMaker.java    From yosegi with Apache License 2.0 4 votes vote down vote up
private void create() throws IOException {
  if ( isCreate ) {
    return;
  }
  int start = columnBinary.binaryStart + ( Float.BYTES * 2 );
  int length = columnBinary.binaryLength - ( Float.BYTES * 2 );

  ICompressor compressor = FindCompressor.get( columnBinary.compressorClassName );
  byte[] binary = compressor.decompress( columnBinary.binary , start , length );

  ByteBuffer wrapBuffer = ByteBuffer.wrap( binary , 0 , binary.length );

  ByteOrder order = wrapBuffer.get() == (byte)0
      ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  final int startIndex = wrapBuffer.getInt();
  int rowCount = wrapBuffer.getInt();
  int nullIndexLength = wrapBuffer.getInt();
  int indexLength = wrapBuffer.getInt();
  int dicLength = binary.length - META_LENGTH - nullIndexLength - indexLength;
  int dicSize = dicLength / Float.BYTES;

  NumberToBinaryUtils.IIntConverter indexConverter =
      NumberToBinaryUtils.getIntConverter( 0 , dicSize );

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

  IReadSupporter indexReader =
      indexConverter.toReadSupporter( binary , META_LENGTH + nullIndexLength , indexLength );
  int[] indexArray = new int[isNullArray.length];
  for ( int i = 0 ; i < indexArray.length ; i++ ) {
    if ( ! isNullArray[i] ) {
      indexArray[i] = indexReader.getInt();
    }
  }

  IReadSupporter dicReader = ByteBufferSupporterFactory.createReadSupporter(
      binary,
      META_LENGTH + nullIndexLength + indexLength,
      dicLength,
      order );
  PrimitiveObject[] dicArray = new PrimitiveObject[dicSize];
  for ( int i = 0 ; i < dicArray.length ; i++ ) {
    dicArray[i] = new FloatObj( dicReader.getFloat() );
  }

  column = new PrimitiveColumn( columnBinary.columnType , columnBinary.columnName );
  column.setCellManager( new OptimizedNullArrayDicCellManager(
      columnBinary.columnType , startIndex , isNullArray , indexArray , dicArray ) );

  isCreate = true;
}
 
Example 19
Source File: ImageHeaderParser.java    From giffun with Apache License 2.0 4 votes vote down vote up
private static int parseExifSegment(RandomAccessReader segmentData) {
    final int headerOffsetSize = JPEG_EXIF_SEGMENT_PREAMBLE.length();

    short byteOrderIdentifier = segmentData.getInt16(headerOffsetSize);
    final ByteOrder byteOrder;
    if (byteOrderIdentifier == MOTOROLA_TIFF_MAGIC_NUMBER) {
        byteOrder = ByteOrder.BIG_ENDIAN;
    } else if (byteOrderIdentifier == INTEL_TIFF_MAGIC_NUMBER) {
        byteOrder = ByteOrder.LITTLE_ENDIAN;
    } else {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Unknown endianness = " + byteOrderIdentifier);
        }
        byteOrder = ByteOrder.BIG_ENDIAN;
    }

    segmentData.order(byteOrder);

    int firstIfdOffset = segmentData.getInt32(headerOffsetSize + 4) + headerOffsetSize;
    int tagCount = segmentData.getInt16(firstIfdOffset);

    int tagOffset, tagType, formatCode, componentCount;
    for (int i = 0; i < tagCount; i++) {
        tagOffset = calcTagOffset(firstIfdOffset, i);

        tagType = segmentData.getInt16(tagOffset);

        // We only want orientation.
        if (tagType != ORIENTATION_TAG_TYPE) {
            continue;
        }

        formatCode = segmentData.getInt16(tagOffset + 2);

        // 12 is max format code.
        if (formatCode < 1 || formatCode > 12) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Got invalid format code=" + formatCode);
            }
            continue;
        }

        componentCount = segmentData.getInt32(tagOffset + 4);

        if (componentCount < 0) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Negative tiff component count");
            }
            continue;
        }

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Got tagIndex=" + i + " tagType=" + tagType + " formatCode=" + formatCode
                    + " componentCount=" + componentCount);
        }

        final int byteCount = componentCount + BYTES_PER_FORMAT[formatCode];

        if (byteCount > 4) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Got byte count > 4, not orientation, continuing, formatCode=" + formatCode);
            }
            continue;
        }

        final int tagValueOffset = tagOffset + 8;

        if (tagValueOffset < 0 || tagValueOffset > segmentData.length()) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Illegal tagValueOffset=" + tagValueOffset + " tagType=" + tagType);
            }
            continue;
        }

        if (byteCount < 0 || tagValueOffset + byteCount > segmentData.length()) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Illegal number of bytes for TI tag data tagType=" + tagType);
            }
            continue;
        }

        //assume componentCount == 1 && fmtCode == 3
        return segmentData.getInt16(tagValueOffset);
    }

    return -1;
}
 
Example 20
Source File: DirectChannelBufferFactory.java    From android-netty with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new factory whose default {@link ByteOrder} is
 * {@link ByteOrder#BIG_ENDIAN}.
 */
public DirectChannelBufferFactory() {
    this(ByteOrder.BIG_ENDIAN);
}