Java Code Examples for org.apache.avro.io.Decoder#readFixed()

The following examples show how to use org.apache.avro.io.Decoder#readFixed() . 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: ByteBufferBackedPrimitiveFloatList.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Instantiate (or re-use) and populate a {@link ByteBufferBackedPrimitiveFloatList} from a {@link org.apache.avro.io.Decoder}.
 *
 * N.B.: the caller must ensure the data is of the appropriate type by calling {@link #isFloatArray(Schema)}.
 *
 * @param old old {@link ByteBufferBackedPrimitiveFloatList} to reuse
 * @param in {@link org.apache.avro.io.Decoder} to read new list from
 * @return a {@link ByteBufferBackedPrimitiveFloatList} with data, possibly the old argument reused
 * @throws IOException on io errors
 */
public static Object readPrimitiveFloatArray(Object old, Decoder in) throws IOException {
  long length = in.readArrayStart();
  long totalLength = 0;

  if (length > 0) {
    ByteBufferBackedPrimitiveFloatList array = (ByteBufferBackedPrimitiveFloatList) newPrimitiveFloatArray(old);
    int index = 0;

    do {
      long byteSize = length * FLOAT_SIZE;
      ByteBuffer byteBuffer = array.byteBuffer.allocate(index++, (int)byteSize);
      in.readFixed(byteBuffer.array(), 0, (int)byteSize);
      totalLength += length;
      length = in.arrayNext();
    } while (length > 0);

    array.byteBuffer.setByteBufferCount(index);
    setupElements(array, (int)totalLength);
    return array;
  } else {
    return new ByteBufferBackedPrimitiveFloatList(0);
  }
}
 
Example 2
Source File: SparkValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public UTF8String read(Decoder decoder, Object reuse) throws IOException {
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();

  decoder.readFixed(buffer.array(), 0, 16);
  long mostSigBits = buffer.getLong();
  long leastSigBits = buffer.getLong();

  return UTF8String.fromString(new UUID(mostSigBits, leastSigBits).toString());
}
 
Example 3
Source File: ValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public UUID read(Decoder decoder, Object ignored) throws IOException {
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();

  decoder.readFixed(buffer.array(), 0, 16);
  long mostSigBits = buffer.getLong();
  long leastSigBits = buffer.getLong();

  return new UUID(mostSigBits, leastSigBits);
}
 
Example 4
Source File: ValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] read(Decoder decoder, Object reuse) throws IOException {
  if (reuse instanceof byte[]) {
    byte[] reusedBytes = (byte[]) reuse;
    if (reusedBytes.length == length) {
      decoder.readFixed(reusedBytes, 0, length);
      return reusedBytes;
    }
  }

  byte[] bytes = new byte[length];
  decoder.readFixed(bytes, 0, length);
  return bytes;
}
 
Example 5
Source File: SparkValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public UTF8String read(Decoder decoder, Object reuse) throws IOException {
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();

  decoder.readFixed(buffer.array(), 0, 16);
  long mostSigBits = buffer.getLong();
  long leastSigBits = buffer.getLong();

  return UTF8String.fromString(new UUID(mostSigBits, leastSigBits).toString());
}
 
Example 6
Source File: ValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public UUID read(Decoder decoder, Object ignored) throws IOException {
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();

  decoder.readFixed(buffer.array(), 0, 16);
  long mostSigBits = buffer.getLong();
  long leastSigBits = buffer.getLong();

  return new UUID(mostSigBits, leastSigBits);
}
 
Example 7
Source File: ValueReaders.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] read(Decoder decoder, Object reuse) throws IOException {
  if (reuse instanceof byte[]) {
    byte[] reusedBytes = (byte[]) reuse;
    if (reusedBytes.length == length) {
      decoder.readFixed(reusedBytes, 0, length);
      return reusedBytes;
    }
  }

  byte[] bytes = new byte[length];
  decoder.readFixed(bytes, 0, length);
  return bytes;
}