default <T> T readObject(Class<T> componentType) { if (BytesMarshallable.class.isAssignableFrom(componentType)) { BytesMarshallable bm = (BytesMarshallable) ObjectUtils.newInstance(componentType); bm.readMarshallable(this); return (T) bm; } if (Enum.class.isAssignableFrom(componentType)) { return (T) readEnum((Class) componentType); } switch (componentType.getName()) { case "java.lang.String": return (T) readUtf8(); case "java.lang.Double": return (T) (Double) readDouble(); case "java.lang.Long": return (T) (Long) readLong(); case "java.lang.Integer": return (T) (Integer) readInt(); default: throw new UnsupportedOperationException("Unsupported " + componentType); } } }
@Override public void readMarshallable(BytesIn bytes) { bs.writeInt(offset + 0, ((bs.readInt(offset + 0)) & 0xFF000000) | (bytes.readInt())); bs.writeLong(offset + 3, ((bs.readLong(offset + 3)) & 0xFFFFFF0000000000L) | (bytes.readLong())); bs.writeLong(offset + 8, ((bs.readLong(offset + 8)) & 0xFFFFFF0000000000L) | (bytes.readLong())); bs.writeInt(offset + 13, (int) (bytes.readLong())); bs.writeLong(offset + 17, bytes.readLong()); }
@Override public void readMarshallable(BytesIn bytes) { bs.writeInt(offset + 0, ((bs.readInt(offset + 0)) & 0xFF000000) | (bytes.readInt())); bs.writeLong(offset + 3, ((bs.readLong(offset + 3)) & 0xFFFFFF0000000000L) | (bytes.readLong())); bs.writeLong(offset + 8, ((bs.readLong(offset + 8)) & 0xFFFFFF0000000000L) | (bytes.readLong())); bs.writeInt(offset + 13, (int) (bytes.readLong())); bs.writeLong(offset + 17, bytes.readLong()); bs.writeInt(offset + 25, bytes.readInt()); bs.writeByte(offset + 29, (byte) (bytes.readInt())); for (int index = 0; index < 128; index++) { setModificationIteratorInitAt(index, bytes.readBoolean()); } }
@Override public void readMarshallable(BytesIn bytes) throws IORuntimeException { long position = bytes.readPosition(); long capacity = bytes.readLong(); long used = bytes.readLong(); if (capacity < 0 || capacity > bytes.readRemaining() >> 3) throw new IORuntimeException("Corrupt used capacity"); if (used < 0 || used > capacity) throw new IORuntimeException("Corrupt used value"); bytes.readSkip(capacity * 8); long length = bytes.readPosition() - position; bytesStore(((Bytes) bytes).bytesStore(), position, length); }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException { field.setInt(o, read.readInt()); } }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException { field.setLong(o, read.readLong()); } }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException, IORuntimeException { @NotNull Bytes bytes = (Bytes) field.get(o); long stopBit = read.readStopBit(); if (stopBit == -1) { if (bytes != null) bytes.release(); field.set(o, null); return; } int length = Maths.toUInt31(stopBit); @NotNull Bytes bs; if (bytes == null) { bs = Bytes.elasticHeapByteBuffer(length); } else { bs = bytes; } Object uo = bs.underlyingObject(); if (uo instanceof ByteBuffer && !(bs.bytesStore() instanceof NativeBytesStore)) { read.read(((ByteBuffer) uo).array(), 0, length); } else { bs.clear(); read.read(bs, length); } bs.readLimit(length); field.set(o, bs); } }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException { field.setBoolean(o, read.readBoolean()); } }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException, IORuntimeException { @Nullable String s = read.readUtf8(); field.set(o, ObjectUtils.convertTo(field.getType(), s)); } }
private void copy(@NotNull BytesIn from, @NotNull BytesOut to) { long copied = from.copyTo((BytesStore) to); to.writeSkip(copied); }
default void uncompress(@NotNull BytesIn from, @NotNull BytesOut to) throws IORuntimeException { try (InputStream input = decompressingStream(from.inputStream())) { to.copyFrom(input); } catch (IOException e) { throw new IORuntimeException(e); } }
default void compress(@NotNull BytesIn from, @NotNull BytesOut to) { try (OutputStream output = compressingStream(to.outputStream())) { from.copyTo(output); } catch (IOException e) { throw new AssertionError(e); // compressing in memory } }