@Override public void writeString(Utf8 utf8) throws IOException { writeBytes(utf8.getBytes(), 0, utf8.getByteLength()); }
@Override public Utf8 readString(Utf8 old) throws IOException { int length = readInt(); Utf8 result = (old != null ? old : new Utf8()); result.setByteLength(length); if (length > 0) { in.readFully(result.getBytes(), 0, length); } return result; }
@Override public void writeString(Utf8 utf8) throws IOException { this.writeBytes(utf8.getBytes(), 0, utf8.getByteLength()); }
@Override public void writeString(Utf8 utf8) throws IOException { this.writeBytes(utf8.getBytes(), 0, utf8.getByteLength()); }
@Override public Iterable<ObjectStoreDeleteOperation> convertRecord(Class<?> outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { Optional<Object> fieldValue = AvroUtils.getFieldValue(inputRecord, this.objectIdField); byte[] objectId; if (fieldValue.isPresent()) { if (fieldValue.get() instanceof Utf8) { objectId = ((Utf8) fieldValue.get()).getBytes(); } else if (fieldValue.get() instanceof String) { objectId = ((String) fieldValue.get()).getBytes(Charsets.UTF_8); } else if (fieldValue.get() instanceof Long) { objectId = Longs.toByteArray((Long) fieldValue.get()); } else if (fieldValue.get() instanceof Integer) { objectId = Ints.toByteArray((Integer) fieldValue.get()); } else { objectId = (byte[]) fieldValue.get(); } return new SingleRecordIterable<ObjectStoreDeleteOperation>(ObjectStoreOperationBuilder.deleteBuilder() .withObjectId(objectId).build()); } else { throw new DataConversionException(String.format("Object Id field %s not found in record %s", this.objectIdField, inputRecord)); } } }
@Override public ByteBuffer readBytes(ByteBuffer old) throws IOException { Symbol actual = parser.advance(Symbol.BYTES); if (actual == Symbol.STRING) { Utf8 s = in.readString(null); return ByteBuffer.wrap(s.getBytes(), 0, s.getByteLength()); } else { assert actual == Symbol.BYTES; return in.readBytes(old); } }
@Override public Utf8 readString(Utf8 old) throws IOException { long length = readLong(); if (length > MAX_ARRAY_SIZE) { throw new UnsupportedOperationException("Cannot read strings longer than " + MAX_ARRAY_SIZE + " bytes"); } if (length < 0L) { throw new AvroRuntimeException("Malformed data. Length is negative: " + length); } Utf8 result = (old != null ? old : new Utf8()); result.setByteLength((int) length); if (0L != length) { doReadBytes(result.getBytes(), 0, (int) length); } return result; }
@Override public ByteBuffer readBytes(ByteBuffer old) throws IOException { Symbol actual = parser.advance(Symbol.BYTES); if (actual == Symbol.STRING) { Utf8 s = in.readString(null); return ByteBuffer.wrap(s.getBytes(), 0, s.getByteLength()); } else { assert actual == Symbol.BYTES; return in.readBytes(old); } }
@Override public Utf8 readString(Utf8 old) throws IOException { int length = readInt(); Utf8 result = (old != null ? old : new Utf8()); result.setByteLength(length); if (0 != length) { doReadBytes(result.getBytes(), 0, length); } return result; }
@Override public void writeString(Utf8 utf8) throws IOException { encodeString(utf8.getBytes(), 0, utf8.getByteLength()); }
@Test public void testByteConstructor() throws Exception { byte[] bs = "Foo".getBytes(StandardCharsets.UTF_8); Utf8 u = new Utf8(bs); assertEquals(bs.length, u.getLength()); for (int i=0; i<bs.length; i++) { assertEquals(bs[i], u.getBytes()[i]); } }
@Test public void testArrayReusedWhenLargerThanRequestedSize() { byte[] bs = "55555".getBytes(StandardCharsets.UTF_8); Utf8 u = new Utf8(bs); assertEquals(5, u.getByteLength()); byte[] content = u.getBytes(); u.setByteLength(3); assertEquals(3, u.getByteLength()); assertSame(content, u.getBytes()); u.setByteLength(4); assertEquals(4, u.getByteLength()); assertSame(content, u.getBytes()); } }
break; case STRING: dout.addStrCol(cIdx, bs.set(((Utf8) value).getBytes())); break; case NULL:
@Override public byte[] serializeKeyAsColumnKeyToBytes(String fieldName, CharSequence columnKey) { if (columnKey.getClass().isAssignableFrom(String.class)) { return ((String) columnKey).getBytes(); } else if (columnKey.getClass().isAssignableFrom(Utf8.class)) { return ((Utf8) columnKey).getBytes(); } else { return columnKey.toString().getBytes(); } }
/** * Strings are encoded by fetching the bytes of the UTF8 encoding, and using * the writeBytes method of this class. * * @param utf8 * The utf8 string to encode. */ @Override public void writeString(Utf8 utf8) throws IOException { writeBytes(utf8.getBytes(), 0, utf8.getByteLength()); }
/** * Strings are encoded by fetching the bytes of the UTF8 encoding, and using * the writeBytes method of this class. * * @param utf8 * The utf8 string to encode. */ @Override public void writeString(Utf8 utf8) throws IOException { writeBytes(utf8.getBytes(), 0, utf8.getByteLength()); }
private Binary fromAvroString(Object value) { if (value instanceof Utf8) { Utf8 utf8 = (Utf8) value; return Binary.fromReusedByteArray(utf8.getBytes(), 0, utf8.getByteLength()); } return Binary.fromCharSequence((CharSequence) value); }
@Override public Utf8 readString(Utf8 old) throws IOException { int length = readInt(); Utf8 result = (old != null ? old : new Utf8()); result.setByteLength(length); if (0 != length) { doReadBytes(result.getBytes(), 0, length); } return result; }
@Override public Utf8 readString(Utf8 old) throws IOException { int length = readInt(); Utf8 result = (old != null ? old : new Utf8()); result.setLength(length); doReadBytes(result.getBytes(), 0, length); return result; }
@Override public Utf8 readString(Utf8 old) throws IOException { int length = readInt(); Utf8 result = (old != null ? old : new Utf8()); result.setLength(length); doReadBytes(result.getBytes(), 0, length); return result; }