Java Code Examples for org.apache.avro.io.Encoder#writeFixed()

The following examples show how to use org.apache.avro.io.Encoder#writeFixed() . 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: SparkValueWriters.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Override
public void write(Decimal d, Encoder encoder) throws IOException {
  Preconditions.checkArgument(d.scale() == scale,
      "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, d);
  Preconditions.checkArgument(d.precision() <= precision,
      "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, d);

  BigDecimal decimal = d.toJavaBigDecimal();

  byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);
  byte[] unscaled = decimal.unscaledValue().toByteArray();
  byte[] buf = bytes.get();
  int offset = length - unscaled.length;

  for (int i = 0; i < length; i += 1) {
    if (i < offset) {
      buf[i] = fillByte;
    } else {
      buf[i] = unscaled[i - offset];
    }
  }

  encoder.writeFixed(buf);
}
 
Example 2
Source File: ValueWriters.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Override
public void write(BigDecimal decimal, Encoder encoder) throws IOException {
  Preconditions.checkArgument(decimal.scale() == scale,
      "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, decimal);
  Preconditions.checkArgument(decimal.precision() <= precision,
      "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, decimal);

  byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);
  byte[] unscaled = decimal.unscaledValue().toByteArray();
  byte[] buf = bytes.get();
  int offset = length - unscaled.length;

  for (int i = 0; i < length; i += 1) {
    if (i < offset) {
      buf[i] = fillByte;
    } else {
      buf[i] = unscaled[i - offset];
    }
  }

  encoder.writeFixed(buf);
}
 
Example 3
Source File: SparkValueWriters.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Override
public void write(Decimal d, Encoder encoder) throws IOException {
  Preconditions.checkArgument(d.scale() == scale,
      "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, d);
  Preconditions.checkArgument(d.precision() <= precision,
      "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, d);

  BigDecimal decimal = d.toJavaBigDecimal();

  byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);
  byte[] unscaled = decimal.unscaledValue().toByteArray();
  byte[] buf = bytes.get();
  int offset = length - unscaled.length;

  for (int i = 0; i < length; i += 1) {
    if (i < offset) {
      buf[i] = fillByte;
    } else {
      buf[i] = unscaled[i - offset];
    }
  }

  encoder.writeFixed(buf);
}
 
Example 4
Source File: ValueWriters.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Override
public void write(BigDecimal decimal, Encoder encoder) throws IOException {
  Preconditions.checkArgument(decimal.scale() == scale,
      "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, decimal);
  Preconditions.checkArgument(decimal.precision() <= precision,
      "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, decimal);

  byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);
  byte[] unscaled = decimal.unscaledValue().toByteArray();
  byte[] buf = bytes.get();
  int offset = length - unscaled.length;

  for (int i = 0; i < length; i += 1) {
    if (i < offset) {
      buf[i] = fillByte;
    } else {
      buf[i] = unscaled[i - offset];
    }
  }

  encoder.writeFixed(buf);
}
 
Example 5
Source File: SparkValueWriters.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public void write(UTF8String s, Encoder encoder) throws IOException {
  // TODO: direct conversion from string to byte buffer
  UUID uuid = UUID.fromString(s.toString());
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();
  buffer.putLong(uuid.getMostSignificantBits());
  buffer.putLong(uuid.getLeastSignificantBits());
  encoder.writeFixed(buffer.array());
}
 
Example 6
Source File: ValueWriters.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public void write(UUID uuid, Encoder encoder) throws IOException {
  // TODO: direct conversion from string to byte buffer
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();
  buffer.putLong(uuid.getMostSignificantBits());
  buffer.putLong(uuid.getLeastSignificantBits());
  encoder.writeFixed(buffer.array());
}
 
Example 7
Source File: SparkValueWriters.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public void write(UTF8String s, Encoder encoder) throws IOException {
  // TODO: direct conversion from string to byte buffer
  UUID uuid = UUID.fromString(s.toString());
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();
  buffer.putLong(uuid.getMostSignificantBits());
  buffer.putLong(uuid.getLeastSignificantBits());
  encoder.writeFixed(buffer.array());
}
 
Example 8
Source File: ValueWriters.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public void write(UUID uuid, Encoder encoder) throws IOException {
  // TODO: direct conversion from string to byte buffer
  ByteBuffer buffer = BUFFER.get();
  buffer.rewind();
  buffer.putLong(uuid.getMostSignificantBits());
  buffer.putLong(uuid.getLeastSignificantBits());
  encoder.writeFixed(buffer.array());
}
 
Example 9
Source File: ValueWriters.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
public void write(byte[] bytes, Encoder encoder) throws IOException {
  Preconditions.checkArgument(bytes.length == length,
      "Cannot write byte array of length %s as fixed[%s]", bytes.length, length);
  encoder.writeFixed(bytes);
}
 
Example 10
Source File: ValueWriters.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
public void write(GenericData.Fixed datum, Encoder encoder) throws IOException {
  Preconditions.checkArgument(datum.bytes().length == length,
      "Cannot write byte array of length %s as fixed[%s]", datum.bytes().length, length);
  encoder.writeFixed(datum.bytes());
}
 
Example 11
Source File: ValueWriters.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
public void write(byte[] bytes, Encoder encoder) throws IOException {
  Preconditions.checkArgument(bytes.length == length,
      "Cannot write byte array of length %s as fixed[%s]", bytes.length, length);
  encoder.writeFixed(bytes);
}
 
Example 12
Source File: ValueWriters.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
public void write(GenericData.Fixed datum, Encoder encoder) throws IOException {
  Preconditions.checkArgument(datum.bytes().length == length,
      "Cannot write byte array of length %s as fixed[%s]", datum.bytes().length, length);
  encoder.writeFixed(datum.bytes());
}