Use new buffers

This commit is contained in:
Andrea Cavalli 2023-03-06 12:19:08 +01:00
parent 6037a906dc
commit 404092106b
108 changed files with 160 additions and 4249 deletions

View File

@ -16,7 +16,7 @@
<lucene.version>9.5.0</lucene.version>
<rocksdb.version>7.9.2</rocksdb.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<data.generator.version>1.0.252</data.generator.version>
<data.generator.version>1.0.258</data.generator.version>
</properties>
<repositories>
<repository>

View File

@ -131,7 +131,7 @@ customTypesData:
serializer: it.cavallium.dbengine.database.remote.LLSnapshotSerializer
Bytes:
javaClass: it.cavallium.dbengine.buffers.Buf
javaClass: it.cavallium.buffer.Buf
serializer: it.cavallium.dbengine.database.remote.BufSerializer
StringMap:
javaClass: java.util.Map<java.lang.String, java.lang.String>

View File

@ -1,182 +0,0 @@
package it.cavallium.dbengine.buffers;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.bytes.ByteList;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.RandomAccess;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.warp.commonutils.stream.SafeByteArrayInputStream;
import org.warp.commonutils.stream.SafeByteArrayOutputStream;
import org.warp.commonutils.stream.SafeDataOutput;
public interface Buf extends ByteList, RandomAccess {
static Buf wrap(ByteList bytes) {
if (bytes instanceof Buf buf) {
return buf;
} else if (bytes instanceof ByteArrayList byteArrayList) {
return ByteListBuf.wrap(byteArrayList.elements(), byteArrayList.size());
} else {
return ByteListBuf.wrap(bytes.toByteArray());
}
}
static Buf wrap(ByteList bytes, int from, int to) {
if (bytes instanceof Buf buf) {
return buf.subList(from, to);
} else if (bytes instanceof ByteArrayList byteArrayList) {
return ByteListBuf.wrap(byteArrayList.elements(), byteArrayList.size()).subList(from, to);
} else {
return ByteListBuf.wrap(bytes.toByteArray()).subList(from, to);
}
}
static Buf wrap(byte[] bytes) {
return ByteListBuf.wrap(bytes);
}
static Buf wrap(byte[] bytes, int from, int to) {
return ByteListBuf.wrap(bytes, to).subList(from, to);
}
static Buf create(int initialCapacity) {
return new ByteListBuf(initialCapacity);
}
static Buf copyOf(byte[] original) {
return new ByteListBuf(original);
}
static Buf create() {
return new ByteListBuf();
}
static Buf wrap(byte[] array, int length) {
return ByteListBuf.wrap(array, length);
}
static Buf createZeroes(int length) {
return ByteListBuf.wrap(new byte[length], length);
}
/**
* Get this element as an array, converting it if needed
*/
byte @NotNull[] asArray();
/**
* Get this element as an array, only if it's already an array, otherwise return null
*/
byte @Nullable[] asArrayStrict();
/**
* Get this element as an array with equal or bigger size, converting it if needed
* The returned array may be bigger than expected!
*/
byte @Nullable[] asUnboundedArray();
/**
* Get this element as an array with equal or bigger size, only if it's already an array, otherwise return null
* The returned array may be bigger than expected!
*/
byte @Nullable[] asUnboundedArrayStrict();
boolean isMutable();
void freeze();
@Override
Buf subList(int from, int to);
Buf copy();
SafeByteArrayInputStream binaryInputStream();
void writeTo(SafeDataOutput dataOutput);
default long getLong(int i) {
return Longs.fromBytes(getByte(i),
getByte(i + 1),
getByte(i + 2),
getByte(i + 3),
getByte(i + 4),
getByte(i + 5),
getByte(i + 6),
getByte(i + 7)
);
}
default int getInt(int i) {
return Ints.fromBytes(getByte(i),
getByte(i + 1),
getByte(i + 2),
getByte(i + 3)
);
}
default float getFloat(int i) {
return Float.intBitsToFloat(getInt(i));
}
default double getDouble(int i) {
return Double.longBitsToDouble(getLong(i));
}
default boolean getBoolean(int i) {
return getByte(i) != 0;
}
default void setBoolean(int i, boolean val) {
set(i, val ? (byte) 1 : 0);
}
default void setByte(int i, byte val) {
set(i, val);
}
default void setInt(int i, int val) {
set(i, (byte) (val >> 24));
set(i + 1, (byte) (val >> 16));
set(i + 2, (byte) (val >> 8));
set(i + 3, (byte) val);
}
default void setLong(int i, long val) {
set(i, (byte) (val >> 56));
set(i + 1, (byte) (val >> 48));
set(i + 2, (byte) (val >> 40));
set(i + 3, (byte) (val >> 32));
set(i + 4, (byte) (val >> 24));
set(i + 5, (byte) (val >> 16));
set(i + 6, (byte) (val >> 8));
set(i + 7, (byte) val);
}
default void setFloat(int i, float val) {
setInt(i, Float.floatToRawIntBits(val));
}
default void setDouble(int i, double val) {
setLong(i, Double.doubleToRawLongBits(val));
}
default SafeByteArrayOutputStream binaryOutputStream() {
return binaryOutputStream(0, size());
}
default SafeByteArrayOutputStream binaryOutputStream(int from) {
return binaryOutputStream(from, size());
}
SafeByteArrayOutputStream binaryOutputStream(int from, int to);
boolean equals(int aStartIndex, Buf b, int bStartIndex, int length);
boolean equals(int aStartIndex, byte[] b, int bStartIndex, int length);
default String toString(Charset charset) {
return new String(this.asArray(), charset);
}
}

View File

@ -1,42 +0,0 @@
package it.cavallium.dbengine.buffers;
import org.jetbrains.annotations.NotNull;
import org.warp.commonutils.stream.SafeByteArrayInputStream;
import org.warp.commonutils.stream.SafeDataInputStream;
public class BufDataInput extends SafeDataInputStream {
/**
* Creates a DataInputStream that uses the specified underlying InputStream.
*
* @param in the specified input stream
*/
private BufDataInput(@NotNull SafeByteArrayInputStream in) {
super(in);
}
public static BufDataInput create(Buf byteList) {
return new BufDataInput(byteList.binaryInputStream());
}
@Deprecated
@Override
public void close() {
}
@Override
public void mark(int readlimit) {
throw new UnsupportedOperationException();
}
@Override
public void reset() {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
}

View File

@ -1,235 +0,0 @@
package it.cavallium.dbengine.buffers;
import static org.warp.commonutils.stream.SafeDataOutputStream.strLen;
import static org.warp.commonutils.stream.SafeDataOutputStream.utfLen;
import it.unimi.dsi.fastutil.Arrays;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.warp.commonutils.stream.SafeByteArrayOutputStream;
import org.warp.commonutils.stream.SafeDataOutputStream;
public class BufDataOutput implements DataOutput {
private final SafeByteArrayOutputStream buf;
private final SafeDataOutputStream dOut;
private final int limit;
private BufDataOutput(SafeByteArrayOutputStream buf) {
this.buf = buf;
this.dOut = new SafeDataOutputStream(buf);
limit = Integer.MAX_VALUE;
}
private BufDataOutput(SafeByteArrayOutputStream buf, int maxSize) {
this.buf = buf;
this.dOut = new SafeDataOutputStream(buf);
this.limit = maxSize;
}
public static BufDataOutput createLimited(int maxSize, int hint) {
if (hint >= 0) {
if (maxSize < 0 || maxSize == Integer.MAX_VALUE) {
return create(hint);
} else {
return new BufDataOutput(new SafeByteArrayOutputStream(Math.min(maxSize, hint)), maxSize);
}
} else {
return createLimited(maxSize);
}
}
public static BufDataOutput createLimited(int maxSize) {
if (maxSize < 0 || maxSize == Integer.MAX_VALUE) {
return create();
} else {
return new BufDataOutput(new SafeByteArrayOutputStream(maxSize), maxSize);
}
}
public static BufDataOutput create() {
return new BufDataOutput(new SafeByteArrayOutputStream());
}
public static BufDataOutput create(int hint) {
if (hint >= 0) {
return new BufDataOutput(new SafeByteArrayOutputStream(hint));
} else {
return create();
}
}
public static BufDataOutput wrap(Buf buf, int from, int to) {
Arrays.ensureFromTo(buf.size(), from, to);
if (buf.isEmpty()) {
return createLimited(0);
} else {
return new BufDataOutput(buf.binaryOutputStream(from), to - from);
}
}
public static BufDataOutput wrap(Buf buf) {
if (buf.isEmpty()) {
return createLimited(0);
} else {
return new BufDataOutput(buf.binaryOutputStream(), buf.size());
}
}
private IllegalStateException unreachable(IOException ex) {
return new IllegalStateException(ex);
}
@Override
public void write(int b) {
checkOutOfBounds(1);
dOut.write(b);
}
private void checkOutOfBounds(int delta) {
if (dOut.size() + delta > limit) {
throw new IndexOutOfBoundsException(limit);
}
}
@Override
public void write(byte @NotNull [] b) {
checkOutOfBounds(b.length);
dOut.write(b);
}
@Override
public void write(byte @NotNull [] b, int off, int len) {
checkOutOfBounds(Math.max(0, Math.min(b.length - off, len)));
dOut.write(b, off, len);
}
@Override
public void writeBoolean(boolean v) {
checkOutOfBounds(1);
dOut.writeBoolean(v);
}
@Override
public void writeByte(int v) {
checkOutOfBounds(Byte.BYTES);
dOut.writeByte(v);
}
@Override
public void writeShort(int v) {
checkOutOfBounds(Short.BYTES);
dOut.writeShort(v);
}
@Override
public void writeChar(int v) {
checkOutOfBounds(Character.BYTES);
dOut.writeChar(v);
}
@Override
public void writeInt(int v) {
checkOutOfBounds(Integer.BYTES);
dOut.writeInt(v);
}
@Override
public void writeLong(long v) {
checkOutOfBounds(Long.BYTES);
dOut.writeLong(v);
}
@Override
public void writeFloat(float v) {
checkOutOfBounds(Float.BYTES);
dOut.writeFloat(v);
}
@Override
public void writeDouble(double v) {
checkOutOfBounds(Double.BYTES);
dOut.writeDouble(v);
}
public void ensureWritable(int size) {
dOut.flush();
buf.ensureWritable(size);
}
@Override
public void writeBytes(@NotNull String s) {
checkOutOfBounds(s.length() * Byte.BYTES);
dOut.writeBytes(s);
}
// todo: check
public void writeBytes(Buf deserialized) {
checkOutOfBounds(deserialized.size());
deserialized.writeTo(dOut);
}
public void writeBytes(byte[] b, int off, int len) {
write(b, off, len);
}
@Override
public void writeChars(@NotNull String s) {
checkOutOfBounds(Character.BYTES * s.length());
dOut.writeChars(s);
}
private static String tooLongMsg(String s, int bits32) {
int slen = s.length();
String head = s.substring(0, 8);
String tail = s.substring(slen - 8, slen);
// handle int overflow with max 3x expansion
long actualLength = (long)slen + Integer.toUnsignedLong(bits32 - slen);
return "encoded string (" + head + "..." + tail + ") too long: "
+ actualLength + " bytes";
}
@Override
public void writeUTF(@NotNull String str) {
var strlen = strLen(str);
var utflen = utfLen(str, strlen);
var bytes = Short.BYTES + utflen;
checkOutOfBounds(bytes);
dOut.writeUTF(strlen, utflen, str);
}
public Buf asList() {
dOut.flush();
return Buf.wrap(this.buf.array, this.buf.length);
}
@Override
public String toString() {
return dOut.toString();
}
@Override
public int hashCode() {
return dOut.hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BufDataOutput that = (BufDataOutput) o;
return Objects.equals(dOut, that.dOut);
}
public int size() {
return dOut.size();
}
}

View File

@ -1,467 +0,0 @@
package it.cavallium.dbengine.buffers;
import it.unimi.dsi.fastutil.bytes.AbstractByteList;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.bytes.ByteCollection;
import it.unimi.dsi.fastutil.bytes.ByteConsumer;
import it.unimi.dsi.fastutil.bytes.ByteIterator;
import it.unimi.dsi.fastutil.bytes.ByteIterators;
import it.unimi.dsi.fastutil.bytes.ByteList;
import it.unimi.dsi.fastutil.bytes.ByteListIterator;
import it.unimi.dsi.fastutil.bytes.ByteSpliterator;
import it.unimi.dsi.fastutil.bytes.ByteSpliterators;
import java.io.Serial;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.warp.commonutils.stream.SafeByteArrayInputStream;
import org.warp.commonutils.stream.SafeByteArrayOutputStream;
import org.warp.commonutils.stream.SafeDataOutput;
class ByteListBuf extends ByteArrayList implements Buf {
private boolean mutable = true;
protected ByteListBuf(byte[] a, boolean wrapped) {
super(a, wrapped);
}
public ByteListBuf(int capacity) {
super(capacity);
}
public ByteListBuf() {
}
public ByteListBuf(Collection<? extends Byte> c) {
super(c);
}
public ByteListBuf(ByteCollection c) {
super(c);
}
public ByteListBuf(ByteList l) {
super(l);
}
public ByteListBuf(byte[] a) {
super(a);
}
public ByteListBuf(byte[] a, int offset, int length) {
super(a, offset, length);
}
public ByteListBuf(Iterator<? extends Byte> i) {
super(i);
}
public ByteListBuf(ByteIterator i) {
super(i);
}
/**
* Wraps a given array into an array list of given size.
*
* <p>
* Note it is guaranteed that the type of the array returned by {@link #elements()} will be the same
* (see the comments in the class documentation).
*
* @param a an array to wrap.
* @param length the length of the resulting array list.
* @return a new array list of the given size, wrapping the given array.
*/
public static ByteListBuf wrap(final byte[] a, final int length) {
if (length > a.length) throw new IllegalArgumentException("The specified length (" + length + ") is greater than the array size (" + a.length + ")");
final ByteListBuf l = new ByteListBuf(a, true);
l.size = length;
return l;
}
/**
* Wraps a given array into an array list.
*
* <p>
* Note it is guaranteed that the type of the array returned by {@link #elements()} will be the same
* (see the comments in the class documentation).
*
* @param a an array to wrap.
* @return a new array list wrapping the given array.
*/
public static ByteListBuf wrap(final byte[] a) {
return wrap(a, a.length);
}
/**
* Creates a new empty array list.
*
* @return a new empty array list.
*/
public static ByteListBuf of() {
return new ByteListBuf();
}
/**
* Creates an array list using an array of elements.
*
* @param init a the array the will become the new backing array of the array list.
* @return a new array list backed by the given array.
* @see #wrap
*/
public static ByteListBuf of(final byte... init) {
return wrap(init);
}
@Override
public byte @NotNull [] asArray() {
if (this.size() == a.length) {
return this.a;
} else {
return this.toByteArray();
}
}
@Override
public byte @Nullable [] asArrayStrict() {
if (this.size() == a.length) {
return a;
} else {
return null;
}
}
@Override
public byte @Nullable [] asUnboundedArray() {
return a;
}
@Override
public byte @Nullable [] asUnboundedArrayStrict() {
return a;
}
@Override
public boolean isMutable() {
return mutable;
}
@Override
public void freeze() {
mutable = false;
}
@Override
public Buf subList(int from, int to) {
if (from == 0 && to == size()) return this;
ensureIndex(from);
ensureIndex(to);
if (from > to) throw new IndexOutOfBoundsException("Start index (" + from + ") is greater than end index (" + to + ")");
return new SubList(from, to);
}
@Override
public Buf copy() {
var copied = ByteListBuf.wrap(this.a.clone());
copied.size = this.size;
return copied;
}
@Override
public SafeByteArrayInputStream binaryInputStream() {
return new SafeByteArrayInputStream(this.a, 0, this.size);
}
@Override
public void writeTo(SafeDataOutput dataOutput) {
dataOutput.write(this.a, 0, this.size);
}
@Override
public SafeByteArrayOutputStream binaryOutputStream(int from, int to) {
it.unimi.dsi.fastutil.Arrays.ensureFromTo(size, from, to);
return new SafeByteArrayOutputStream(a, from, to);
}
@Override
public boolean equals(int aStartIndex, Buf b, int bStartIndex, int length) {
return b.equals(bStartIndex, this.a, aStartIndex, length);
}
@Override
public boolean equals(int aStartIndex, byte[] b, int bStartIndex, int length) {
if (aStartIndex < 0) return false;
if (aStartIndex + length > this.size) {
return false;
}
return Arrays.equals(a, aStartIndex, aStartIndex + length, b, bStartIndex, bStartIndex + length);
}
@Override
public String toString(Charset charset) {
return new String(a, 0, size, charset);
}
private class SubList extends AbstractByteList.ByteRandomAccessSubList implements Buf {
@Serial
private static final long serialVersionUID = -3185226345314976296L;
private boolean subMutable = true;
protected SubList(int from, int to) {
super(ByteListBuf.this, from, to);
}
// Most of the inherited methods should be fine, but we can override a few of them for performance.
// Needed because we can't access the parent class' instance variables directly in a different
// instance of SubList.
private byte[] getParentArray() {
return a;
}
@Override
public @NotNull Buf subList(int from, int to) {
it.unimi.dsi.fastutil.Arrays.ensureFromTo(a.length, from, to);
if (from > to) throw new IllegalArgumentException("Start index (" + from + ") is greater than end index (" + to + ")");
// Sadly we have to rewrap this, because if there is a sublist of a sublist, and the
// subsublist adds, both sublists need to update their "to" value.
return new SubList(from, to);
}
@Override
public Buf copy() {
return Buf.wrap(Arrays.copyOfRange(a, from, to));
}
@Override
public SafeByteArrayInputStream binaryInputStream() {
return new SafeByteArrayInputStream(a, from, size());
}
@Override
public void writeTo(SafeDataOutput dataOutput) {
dataOutput.write(a, from, size());
}
@Override
public SafeByteArrayOutputStream binaryOutputStream(int from, int to) {
it.unimi.dsi.fastutil.Arrays.ensureFromTo(size(), from, to);
return new SafeByteArrayOutputStream(a, from + this.from, to + this.from);
}
@Override
public boolean equals(int aStartIndex, Buf b, int bStartIndex, int length) {
return b.equals(bStartIndex, a, aStartIndex + from, length);
}
@Override
public boolean equals(int aStartIndex, byte[] b, int bStartIndex, int length) {
var aFrom = from + aStartIndex;
var aTo = from + aStartIndex + length;
if (aFrom < from) return false;
if (aTo > to) return false;
return Arrays.equals(a, aFrom, aTo, b, bStartIndex, bStartIndex + length);
}
@Override
public byte getByte(int i) {
ensureRestrictedIndex(i);
return a[i + from];
}
@Override
public byte @NotNull [] asArray() {
if (this.from == 0 && this.to == a.length) {
return a;
} else {
return toByteArray();
}
}
@Override
public byte @Nullable [] asArrayStrict() {
if (this.from == 0 && this.to == a.length) {
return a;
} else {
return null;
}
}
@Override
public byte @Nullable [] asUnboundedArray() {
if (from == 0) {
return a;
} else {
return toByteArray();
}
}
@Override
public byte @Nullable [] asUnboundedArrayStrict() {
if (from == 0) {
return a;
} else {
return null;
}
}
@Override
public boolean isMutable() {
return mutable && subMutable;
}
@Override
public void freeze() {
subMutable = false;
}
private final class SubListIterator extends ByteIterators.AbstractIndexBasedListIterator {
// We are using pos == 0 to be 0 relative to SubList.from (meaning you need to do a[from + i] when
// accessing array).
SubListIterator(int index) {
super(0, index);
}
@Override
protected byte get(int i) {
return a[from + i];
}
@Override
protected void add(int i, byte k) {
ByteListBuf.SubList.this.add(i, k);
}
@Override
protected void set(int i, byte k) {
ByteListBuf.SubList.this.set(i, k);
}
@Override
protected void remove(int i) {
ByteListBuf.SubList.this.removeByte(i);
}
@Override
protected int getMaxPos() {
return to - from;
}
@Override
public byte nextByte() {
if (!hasNext()) throw new NoSuchElementException();
return a[from + (lastReturned = pos++)];
}
@Override
public byte previousByte() {
if (!hasPrevious()) throw new NoSuchElementException();
return a[from + (lastReturned = --pos)];
}
@Override
public void forEachRemaining(final ByteConsumer action) {
final int max = to - from;
while (pos < max) {
action.accept(a[from + (lastReturned = pos++)]);
}
}
}
@Override
public @NotNull ByteListIterator listIterator(int index) {
return new ByteListBuf.SubList.SubListIterator(index);
}
private final class SubListSpliterator extends ByteSpliterators.LateBindingSizeIndexBasedSpliterator {
// We are using pos == 0 to be 0 relative to real array 0
SubListSpliterator() {
super(from);
}
private SubListSpliterator(int pos, int maxPos) {
super(pos, maxPos);
}
@Override
protected int getMaxPosFromBackingStore() {
return to;
}
@Override
protected byte get(int i) {
return a[i];
}
@Override
protected ByteListBuf.SubList.SubListSpliterator makeForSplit(int pos, int maxPos) {
return new ByteListBuf.SubList.SubListSpliterator(pos, maxPos);
}
@Override
public boolean tryAdvance(final ByteConsumer action) {
if (pos >= getMaxPos()) return false;
action.accept(a[pos++]);
return true;
}
@Override
public void forEachRemaining(final ByteConsumer action) {
final int max = getMaxPos();
while (pos < max) {
action.accept(a[pos++]);
}
}
}
@Override
public ByteSpliterator spliterator() {
return new ByteListBuf.SubList.SubListSpliterator();
}
boolean contentsEquals(byte[] otherA, int otherAFrom, int otherATo) {
if (a == otherA && from == otherAFrom && to == otherATo) return true;
return Arrays.equals(a, from, to, otherA, otherAFrom, otherATo);
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null) return false;
if (!(o instanceof java.util.List)) return false;
if (o instanceof ByteListBuf other) {
return contentsEquals(other.a, 0, other.size());
}
if (o instanceof SubList other) {
return contentsEquals(other.getParentArray(), other.from, other.to);
}
return super.equals(o);
}
int contentsCompareTo(byte[] otherA, int otherAFrom, int otherATo) {
if (a == otherA && from == otherAFrom && to == otherATo) return 0;
return Arrays.compareUnsigned(a, from, to, otherA, otherAFrom, otherATo);
}
@Override
public int compareTo(final java.util.@NotNull List<? extends Byte> l) {
if (l instanceof ByteListBuf other) {
return contentsCompareTo(other.a, 0, other.size());
}
if (l instanceof ByteListBuf.SubList other) {
return contentsCompareTo(other.getParentArray(), other.from, other.to);
}
return super.compareTo(l);
}
@Override
public String toString(Charset charset) {
return new String(a, from, to, charset);
}
}
}

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.client;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.rpc.current.data.Column;
import org.jetbrains.annotations.Nullable;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.client;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.Serializer;
import org.jetbrains.annotations.NotNull;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.client;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
import org.jetbrains.annotations.NotNull;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.client.query;
import com.squareup.moshi.JsonAdapter;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.IntOpenHashSetJsonAdapter;
import it.cavallium.dbengine.client.query.current.CurrentVersion;
import it.cavallium.dbengine.client.query.current.IBaseType;

View File

@ -2,7 +2,7 @@ package it.cavallium.dbengine.database;
import static it.cavallium.dbengine.database.LLUtils.unmodifiableBytes;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.util.StringJoiner;
import org.jetbrains.annotations.Nullable;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.database.disk.BinarySerializationFunction;
import it.cavallium.dbengine.database.serialization.KVSerializationFunction;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.util.Objects;
import java.util.StringJoiner;
import org.apache.logging.log4j.LogManager;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.util.StringJoiner;
import org.jetbrains.annotations.Nullable;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.disk.BinarySerializationFunction;
import java.io.IOException;
import org.jetbrains.annotations.Nullable;

View File

@ -4,7 +4,7 @@ import static org.apache.commons.lang3.ArrayUtils.EMPTY_BYTE_ARRAY;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.HitEntry;
import it.cavallium.dbengine.client.HitKey;
import it.cavallium.dbengine.database.serialization.SerializationFunction;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

View File

@ -1,5 +1,5 @@
package it.cavallium.dbengine.database;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
public record SerializedKey<T>(T key, Buf serialized) {}

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.Serializer;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure;
import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure;
import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.Delta;
import it.cavallium.dbengine.database.LLDelta;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.Delta;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.Delta;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import org.jetbrains.annotations.Nullable;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.serialization.Serializer;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.serialization.Serializer;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.serialization.Serializer;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.serialization.Serializer;
public class SubStageGetterSingleBytes extends SubStageGetterSingle<Buf> {

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.Serializer;
import java.util.Map;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.Serializer;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;

View File

@ -6,7 +6,7 @@ import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.RepeatedElementList;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.serialization.SerializationFunction;
import org.jetbrains.annotations.Nullable;

View File

@ -2,7 +2,7 @@ package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.LLUtils.asArray;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.nio.ByteBuffer;
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.RocksDBException;

View File

@ -4,7 +4,7 @@ import static java.lang.Boolean.parseBoolean;
import static java.lang.System.getProperty;
import static java.util.Objects.requireNonNull;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLUtils;
import java.nio.ByteBuffer;
import org.apache.logging.log4j.LogManager;

View File

@ -20,7 +20,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Streams;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.database.ColumnUtils;
import it.cavallium.dbengine.database.LLDelta;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLEntry;
import it.cavallium.dbengine.database.LLRange;
import java.util.function.Supplier;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLEntry;
import it.cavallium.dbengine.database.LLRange;
import java.util.function.Supplier;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import java.util.function.Supplier;
import org.rocksdb.ReadOptions;

View File

@ -4,7 +4,7 @@ import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
import static it.cavallium.dbengine.database.LLUtils.isBoundedRange;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.disk.rocksdb.RocksIteratorObj;

View File

@ -7,7 +7,7 @@ import static it.cavallium.dbengine.utils.StreamUtils.streamWhileNonNull;
import com.google.common.collect.Iterators;
import com.google.common.collect.Streams;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.utils.DBException;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import java.util.function.Supplier;
import org.rocksdb.ReadOptions;

View File

@ -3,7 +3,7 @@ package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
import static it.cavallium.dbengine.utils.StreamUtils.streamWhileNonNull;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLEntry;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;

View File

@ -5,7 +5,7 @@ import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
import static it.cavallium.dbengine.database.LLUtils.isBoundedRange;
import static it.cavallium.dbengine.utils.StreamUtils.streamWhileNonNull;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.disk.rocksdb.RocksIteratorObj;

View File

@ -3,7 +3,7 @@ package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.disk.UpdateAtomicResultMode.DELTA;
import com.google.common.util.concurrent.AbstractScheduledService.Scheduler;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot;

View File

@ -4,7 +4,7 @@ import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.lucene.ExponentialPageLimits;

View File

@ -3,7 +3,7 @@ package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
import io.micrometer.core.instrument.MeterRegistry;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.utils.DBException;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.disk;
import io.micrometer.core.instrument.MeterRegistry;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.disk.rocksdb.RocksIteratorObj;

View File

@ -3,7 +3,7 @@ package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
import io.micrometer.core.instrument.MeterRegistry;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.utils.DBException;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
public record UpdateAtomicResultCurrent(Buf current) implements UpdateAtomicResult {
}

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
public record UpdateAtomicResultPrevious(Buf previous) implements UpdateAtomicResult {

View File

@ -2,7 +2,7 @@ package it.cavallium.dbengine.database.disk.rocksdb;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.utils.SimpleResource;
import java.nio.ByteBuffer;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.memory;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
public class BLRange {

View File

@ -4,7 +4,7 @@ import static it.cavallium.dbengine.utils.StreamUtils.count;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLDictionary;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.memory;
import io.micrometer.core.instrument.MeterRegistry;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.client.MemoryStats;
import it.cavallium.dbengine.database.ColumnProperty;
import it.cavallium.dbengine.database.LLDictionary;

View File

@ -1,6 +1,6 @@
package it.cavallium.dbengine.database.memory;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLDictionaryResultType;
import it.cavallium.dbengine.database.LLSingleton;

View File

@ -1,7 +1,9 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
public class BufSerializer implements DataSerializer<Buf> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Buf bytes) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Buf bytes) {
dataOutput.writeInt(bytes.size());
for (Byte aByte : bytes) {
dataOutput.writeByte(aByte);
@ -18,7 +20,7 @@ public class BufSerializer implements DataSerializer<Buf> {
}
@Override
public @NotNull Buf deserialize(DataInput dataInput) throws IOException {
public @NotNull Buf deserialize(SafeDataInput dataInput) {
var size = dataInput.readInt();
var bal = Buf.create(size);
for (int i = 0; i < size; i++) {

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.rocksdb.ColumnFamilyHandle;
public class ColumnFamilyHandleSerializer implements DataSerializer<ColumnFamilyHandle> {
@Override
public void serialize(DataOutput dataOutput, @NotNull ColumnFamilyHandle columnFamilyHandle) {
public void serialize(SafeDataOutput dataOutput, @NotNull ColumnFamilyHandle columnFamilyHandle) {
throw new UnsupportedOperationException("Can't encode this type");
}
@Override
public @NotNull ColumnFamilyHandle deserialize(DataInput dataInput) {
public @NotNull ColumnFamilyHandle deserialize(SafeDataInput dataInput) {
throw new UnsupportedOperationException("Can't encode this type");
}
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.client.Compression;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class CompressionSerializer implements DataSerializer<Compression> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Compression compression) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Compression compression) {
dataOutput.writeInt(compression.ordinal());
}
@Override
public @NotNull Compression deserialize(DataInput dataInput) throws IOException {
public @NotNull Compression deserialize(SafeDataInput dataInput) {
return Compression.values()[dataInput.readInt()];
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -11,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
public class DurationSerializer implements DataSerializer<Duration> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Duration duration) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Duration duration) {
var units = duration.getUnits();
var smallestUnit = (ChronoUnit) units.get(units.size() - 1);
dataOutput.writeInt(smallestUnit.ordinal());
@ -19,7 +21,7 @@ public class DurationSerializer implements DataSerializer<Duration> {
}
@Override
public @NotNull Duration deserialize(DataInput dataInput) throws IOException {
public @NotNull Duration deserialize(SafeDataInput dataInput) {
var smallestUnit = ChronoUnit.values()[dataInput.readInt()];
return Duration.of(dataInput.readLong(), smallestUnit);
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.database.LLSnapshot;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class LLSnapshotSerializer implements DataSerializer<LLSnapshot> {
@Override
public void serialize(DataOutput dataOutput, @NotNull LLSnapshot llSnapshot) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull LLSnapshot llSnapshot) {
dataOutput.writeLong(llSnapshot.getSequenceNumber());
}
@Override
public @NotNull LLSnapshot deserialize(DataInput dataInput) throws IOException {
public @NotNull LLSnapshot deserialize(SafeDataInput dataInput) {
return new LLSnapshot(dataInput.readLong());
}
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.lucene.LuceneHacks;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,14 +12,14 @@ import org.jetbrains.annotations.NotNull;
public class LuceneHacksSerializer implements DataSerializer<LuceneHacks> {
@Override
public void serialize(DataOutput dataOutput, @NotNull LuceneHacks luceneHacks) {
public void serialize(SafeDataOutput dataOutput, @NotNull LuceneHacks luceneHacks) {
if (luceneHacks.customLocalSearcher() != null || luceneHacks.customMultiSearcher() != null) {
throw new UnsupportedOperationException("Can't encode this type");
}
}
@Override
public @NotNull LuceneHacks deserialize(DataInput dataInput) {
public @NotNull LuceneHacks deserialize(SafeDataInput dataInput) {
return new LuceneHacks(null, null);
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class PathSerializer implements DataSerializer<Path> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Path path) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Path path) {
dataOutput.writeUTF(path.toString());
}
@Override
public @NotNull Path deserialize(DataInput dataInput) throws IOException {
public @NotNull Path deserialize(SafeDataInput dataInput) {
return Path.of(dataInput.readUTF());
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.rocksdb.RocksDB;
public class RocksDBSerializer implements DataSerializer<RocksDB> {
@Override
public void serialize(DataOutput dataOutput, @NotNull RocksDB rocksDB) {
public void serialize(SafeDataOutput dataOutput, @NotNull RocksDB rocksDB) {
throw new UnsupportedOperationException("Can't encode this type");
}
@Override
public @NotNull RocksDB deserialize(DataInput dataInput) {
public @NotNull RocksDB deserialize(SafeDataInput dataInput) {
throw new UnsupportedOperationException("Can't encode this type");
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -11,13 +13,12 @@ import org.rocksdb.ColumnFamilyHandle;
public class String2ColumnFamilyHandleMapSerializer implements DataSerializer<Map<String, ColumnFamilyHandle>> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Map<String, ColumnFamilyHandle> stringColumnFamilyHandleMap)
throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Map<String, ColumnFamilyHandle> stringColumnFamilyHandleMap) {
throw new UnsupportedOperationException("Can't encode this type");
}
@Override
public @NotNull Map<String, ColumnFamilyHandle> deserialize(DataInput dataInput) {
public @NotNull Map<String, ColumnFamilyHandle> deserialize(SafeDataInput dataInput) {
throw new UnsupportedOperationException("Can't encode this type");
}
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -16,8 +18,7 @@ public class String2FieldAnalyzerMapSerializer implements DataSerializer<Map<Str
private static final TextFieldsAnalyzerSerializer TEXT_FIELDS_ANALYZER_SERIALIZER = new TextFieldsAnalyzerSerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Map<String, TextFieldsAnalyzer> stringTextFieldsAnalyzerMap)
throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Map<String, TextFieldsAnalyzer> stringTextFieldsAnalyzerMap) {
dataOutput.writeInt(stringTextFieldsAnalyzerMap.size());
for (Entry<String, TextFieldsAnalyzer> entry : stringTextFieldsAnalyzerMap.entrySet()) {
dataOutput.writeUTF(entry.getKey());
@ -26,7 +27,7 @@ public class String2FieldAnalyzerMapSerializer implements DataSerializer<Map<Str
}
@Override
public @NotNull Map<String, TextFieldsAnalyzer> deserialize(DataInput dataInput) throws IOException {
public @NotNull Map<String, TextFieldsAnalyzer> deserialize(SafeDataInput dataInput) {
var size = dataInput.readInt();
var result = new HashMap<String, TextFieldsAnalyzer>(size);
for (int i = 0; i < size; i++) {

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -16,8 +18,7 @@ public class String2FieldSimilarityMapSerializer implements DataSerializer<Map<S
private static final TextFieldsSimilaritySerializer TEXT_FIELDS_SIMILARITY_SERIALIZER = new TextFieldsSimilaritySerializer();
@Override
public void serialize(DataOutput dataOutput, @NotNull Map<String, TextFieldsSimilarity> stringTextFieldsSimilarityMap)
throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Map<String, TextFieldsSimilarity> stringTextFieldsSimilarityMap) {
dataOutput.writeInt(stringTextFieldsSimilarityMap.size());
for (Entry<String, TextFieldsSimilarity> entry : stringTextFieldsSimilarityMap.entrySet()) {
dataOutput.writeUTF(entry.getKey());
@ -26,7 +27,7 @@ public class String2FieldSimilarityMapSerializer implements DataSerializer<Map<S
}
@Override
public @NotNull Map<String, TextFieldsSimilarity> deserialize(DataInput dataInput) throws IOException {
public @NotNull Map<String, TextFieldsSimilarity> deserialize(SafeDataInput dataInput) {
var size = dataInput.readInt();
var result = new HashMap<String, TextFieldsSimilarity>(size);
for (int i = 0; i < size; i++) {

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -11,13 +13,13 @@ import org.jetbrains.annotations.NotNull;
public class StringEntrySerializer implements DataSerializer<Map.Entry> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Map.Entry entry) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Map.Entry entry) {
dataOutput.writeUTF((String) entry.getKey());
dataOutput.writeUTF((String) entry.getValue());
}
@Override
public @NotNull Map.Entry deserialize(DataInput dataInput) throws IOException {
public @NotNull Map.Entry deserialize(SafeDataInput dataInput) {
return Map.entry(dataInput.readUTF(), dataInput.readUTF());
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -13,8 +15,7 @@ import org.jetbrains.annotations.NotNull;
public class StringMapSerializer implements DataSerializer<Map<String, String>> {
@Override
public void serialize(DataOutput dataOutput, @NotNull Map<String, String> stringTextFieldsAnalyzerMap)
throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull Map<String, String> stringTextFieldsAnalyzerMap) {
dataOutput.writeInt(stringTextFieldsAnalyzerMap.size());
for (Entry<String, String> entry : stringTextFieldsAnalyzerMap.entrySet()) {
dataOutput.writeUTF(entry.getKey());
@ -23,7 +24,7 @@ public class StringMapSerializer implements DataSerializer<Map<String, String>>
}
@Override
public @NotNull Map<String, String> deserialize(DataInput dataInput) throws IOException {
public @NotNull Map<String, String> deserialize(SafeDataInput dataInput) {
var size = dataInput.readInt();
var result = new HashMap<String, String>(size);
for (int i = 0; i < size; i++) {

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class TextFieldsAnalyzerSerializer implements DataSerializer<TextFieldsAnalyzer> {
@Override
public void serialize(DataOutput dataOutput, @NotNull TextFieldsAnalyzer textFieldsAnalyzer) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull TextFieldsAnalyzer textFieldsAnalyzer) {
dataOutput.writeInt(textFieldsAnalyzer.ordinal());
}
@Override
public @NotNull TextFieldsAnalyzer deserialize(DataInput dataInput) throws IOException {
public @NotNull TextFieldsAnalyzer deserialize(SafeDataInput dataInput) {
return TextFieldsAnalyzer.values()[dataInput.readInt()];
}
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class TextFieldsSimilaritySerializer implements DataSerializer<TextFieldsSimilarity> {
@Override
public void serialize(DataOutput dataOutput, @NotNull TextFieldsSimilarity textFieldsSimilarity) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull TextFieldsSimilarity textFieldsSimilarity) {
dataOutput.writeInt(textFieldsSimilarity.ordinal());
}
@Override
public @NotNull TextFieldsSimilarity deserialize(DataInput dataInput) throws IOException {
public @NotNull TextFieldsSimilarity deserialize(SafeDataInput dataInput) {
return TextFieldsSimilarity.values()[dataInput.readInt()];
}
}

View File

@ -2,6 +2,8 @@ package it.cavallium.dbengine.database.remote;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.dbengine.database.UpdateReturnMode;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@ -10,12 +12,12 @@ import org.jetbrains.annotations.NotNull;
public class UpdateReturnModeSerializer implements DataSerializer<UpdateReturnMode> {
@Override
public void serialize(DataOutput dataOutput, @NotNull UpdateReturnMode updateReturnMode) throws IOException {
public void serialize(SafeDataOutput dataOutput, @NotNull UpdateReturnMode updateReturnMode) {
dataOutput.writeInt(updateReturnMode.ordinal());
}
@Override
public @NotNull UpdateReturnMode deserialize(DataInput dataInput) throws IOException {
public @NotNull UpdateReturnMode deserialize(SafeDataInput dataInput) {
return UpdateReturnMode.values()[dataInput.readInt()];
}
}

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.serialization;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;

View File

@ -1,7 +1,7 @@
package it.cavallium.dbengine.database.serialization;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import java.io.IOError;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.serialization;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;

View File

@ -1,8 +1,8 @@
package it.cavallium.dbengine.database.serialization;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;

View File

@ -3,7 +3,7 @@ package it.cavallium.dbengine.utils;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.buffer.Buf;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -1,5 +1,4 @@
module dbengine {
exports org.warp.commonutils.stream;
exports it.cavallium.dbengine.lucene;
exports it.cavallium.dbengine.database;
exports it.cavallium.dbengine.rpc.current.data;
@ -18,7 +17,6 @@ module dbengine {
opens it.cavallium.dbengine.database.remote;
exports it.cavallium.dbengine.utils;
exports it.cavallium.dbengine.database.disk.rocksdb;
exports it.cavallium.dbengine.buffers;
exports it.cavallium.dbengine.lucene.hugepq.search;
requires org.jetbrains.annotations;
requires com.google.common;

View File

@ -1,29 +0,0 @@
package org.warp.commonutils.stream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import org.jetbrains.annotations.NotNull;
/**
* Simple {@link InputStream} implementation that exposes currently
* available content of a {@link ByteBuffer}.
*/
public class ByteBufferBackedInputStream extends InputStream {
protected final ByteBuffer _b;
public ByteBufferBackedInputStream(ByteBuffer buf) { _b = buf; }
@Override public int available() { return _b.remaining(); }
@Override
public int read() { return _b.hasRemaining() ? (_b.get() & 0xFF) : -1; }
@Override
public int read(byte @NotNull [] bytes, int off, int len) {
if (!_b.hasRemaining()) return -1;
len = Math.min(len, _b.remaining());
_b.get(bytes, off, len);
return len;
}
}

View File

@ -1,11 +0,0 @@
package org.warp.commonutils.stream;
import java.io.DataInput;
import java.io.DataOutput;
public interface DataInputOutput extends DataInput, DataOutput {
DataInput getIn();
DataOutput getOut();
}

View File

@ -1,173 +0,0 @@
package org.warp.commonutils.stream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class DataInputOutputImpl implements DataInputOutput {
private final DataInput in;
private final DataOutput out;
public DataInputOutputImpl(DataInput in, DataOutput out) {
this.in = in;
this.out = out;
}
@Override
public DataInput getIn() {
return this;
}
@Override
public DataOutput getOut() {
return this;
}
@Override
public void readFully(byte @NotNull [] bytes) throws IOException {
in.readFully(bytes);
}
@Override
public void readFully(byte @NotNull [] bytes, int i, int i1) throws IOException {
in.readFully(bytes, i, i1);
}
@Override
public int skipBytes(int i) throws IOException {
return in.skipBytes(i);
}
@Override
public boolean readBoolean() throws IOException {
return in.readBoolean();
}
@Override
public byte readByte() throws IOException {
return in.readByte();
}
@Override
public int readUnsignedByte() throws IOException {
return in.readUnsignedByte();
}
@Override
public short readShort() throws IOException {
return in.readShort();
}
@Override
public int readUnsignedShort() throws IOException {
return in.readUnsignedShort();
}
@Override
public char readChar() throws IOException {
return in.readChar();
}
@Override
public int readInt() throws IOException {
return in.readInt();
}
@Override
public long readLong() throws IOException {
return in.readLong();
}
@Override
public float readFloat() throws IOException {
return in.readFloat();
}
@Override
public double readDouble() throws IOException {
return in.readDouble();
}
@Override
public String readLine() throws IOException {
return in.readLine();
}
@NotNull
@Override
public String readUTF() throws IOException {
return in.readUTF();
}
@Override
public void write(int i) throws IOException {
out.write(i);
}
@Override
public void write(byte @NotNull [] bytes) throws IOException {
out.write(bytes);
}
@Override
public void write(byte @NotNull [] bytes, int i, int i1) throws IOException {
out.write(bytes, i, i1);
}
@Override
public void writeBoolean(boolean b) throws IOException {
out.writeBoolean(b);
}
@Override
public void writeByte(int i) throws IOException {
out.writeByte(i);
}
@Override
public void writeShort(int i) throws IOException {
out.writeShort(i);
}
@Override
public void writeChar(int i) throws IOException {
out.writeChar(i);
}
@Override
public void writeInt(int i) throws IOException {
out.writeInt(i);
}
@Override
public void writeLong(long l) throws IOException {
out.writeLong(l);
}
@Override
public void writeFloat(float v) throws IOException {
out.writeFloat(v);
}
@Override
public void writeDouble(double v) throws IOException {
out.writeDouble(v);
}
@Override
public void writeBytes(@NotNull String s) throws IOException {
out.writeBytes(s);
}
@Override
public void writeChars(@NotNull String s) throws IOException {
out.writeChars(s);
}
@Override
public void writeUTF(@NotNull String s) throws IOException {
out.writeUTF(s);
}
}

View File

@ -1,103 +0,0 @@
package org.warp.commonutils.stream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
public class DataInputOutputStream extends DataOutputStream implements DataInputOutput {
private final DataInputStream in;
public DataInputOutputStream(DataInputStream in, DataOutputStream out) {
super(out);
this.in = in;
}
@Override
public DataInputStream getIn() {
return in;
}
@Override
public DataOutputStream getOut() {
return this;
}
@Override
public void readFully(byte @NotNull [] bytes) throws IOException {
in.readFully(bytes);
}
@Override
public void readFully(byte @NotNull [] bytes, int i, int i1) throws IOException {
in.readFully(bytes, i, i1);
}
@Override
public int skipBytes(int i) throws IOException {
return in.skipBytes(i);
}
@Override
public boolean readBoolean() throws IOException {
return in.readBoolean();
}
@Override
public byte readByte() throws IOException {
return in.readByte();
}
@Override
public int readUnsignedByte() throws IOException {
return in.readUnsignedByte();
}
@Override
public short readShort() throws IOException {
return in.readShort();
}
@Override
public int readUnsignedShort() throws IOException {
return in.readUnsignedShort();
}
@Override
public char readChar() throws IOException {
return in.readChar();
}
@Override
public int readInt() throws IOException {
return in.readInt();
}
@Override
public long readLong() throws IOException {
return in.readLong();
}
@Override
public float readFloat() throws IOException {
return in.readFloat();
}
@Override
public double readDouble() throws IOException {
return in.readDouble();
}
@Deprecated
@Override
public String readLine() throws IOException {
return in.readLine();
}
@NotNull
@Override
public String readUTF() throws IOException {
return in.readUTF();
}
}

View File

@ -1,136 +0,0 @@
/*
* Copyright (C) 2005-2022 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.warp.commonutils.stream;
/** Simple, fast and repositionable byte-array input stream.
*
* <p><strong>Warning</strong>: this class implements the correct semantics
* of {@link #read(byte[], int, int)} as described in {@link java.io.InputStream}.
* The implementation given in {@link java.io.ByteArrayInputStream} is broken,
* but it will never be fixed because it's too late.
*
* @author Sebastiano Vigna
*/
public class SafeByteArrayInputStream extends SafeMeasurableInputStream implements SafeRepositionableStream {
/** The array backing the input stream. */
public byte[] array;
/** The first valid entry. */
public int offset;
/** The number of valid bytes in {@link #array} starting from {@link #offset}. */
public int length;
/** The current position as a distance from {@link #offset}. */
private int position;
/** The current mark as a position, or -1 if no mark exists. */
private int mark;
/** Creates a new array input stream using a given array fragment.
*
* @param array the backing array.
* @param offset the first valid entry of the array.
* @param length the number of valid bytes.
*/
public SafeByteArrayInputStream(final byte[] array, final int offset, final int length) {
this.array = array;
this.offset = offset;
this.length = length;
}
/** Creates a new array input stream using a given array.
*
* @param array the backing array.
*/
public SafeByteArrayInputStream(final byte[] array) {
this(array, 0, array.length);
}
@Override
public boolean markSupported() {
return true;
}
@Override
public void reset() {
position = mark;
}
/** Closing a fast byte array input stream has no effect. */
@Override
public void close() {}
@Override
public void mark(final int dummy) {
mark = position;
}
@Override
public int available() {
return length - position;
}
@Override
public long skip(long n) {
if (n <= length - position) {
position += (int)n;
return n;
}
n = length - position;
position = length;
return n;
}
@Override
public int read() {
if (length == position) return -1;
return array[offset + position++] & 0xFF;
}
/** Reads bytes from this byte-array input stream as
* specified in {@link java.io.InputStream#read(byte[], int, int)}.
* Note that the implementation given in {@link java.io.ByteArrayInputStream#read(byte[], int, int)}
* will return -1 on a zero-length read at EOF, contrarily to the specification. We won't.
*/
@Override
public int read(final byte b[], final int offset, final int length) {
if (this.length == this.position) return length == 0 ? 0 : -1;
final int n = Math.min(length, this.length - this.position);
System.arraycopy(array, this.offset + this.position, b, offset, n);
this.position += n;
return n;
}
@Override
public long position() {
return position;
}
@Override
public void position(final long newPosition) {
position = (int)Math.min(newPosition, length);
}
@Override
public long length() {
return length;
}
}

View File

@ -1,155 +0,0 @@
/*
* Copyright (C) 2005-2022 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.warp.commonutils.stream;
import it.unimi.dsi.fastutil.Arrays;
import it.unimi.dsi.fastutil.bytes.ByteArrays;
/** Simple, fast byte-array output stream that exposes the backing array.
*
* <p>{@link java.io.ByteArrayOutputStream} is nice, but to get its content you
* must generate each time a new object. This doesn't happen here.
*
* <p>This class will automatically enlarge the backing array, doubling its
* size whenever new space is needed. The {@link #reset()} method will
* mark the content as empty, but will not decrease the capacity: use
* {@link #trim()} for that purpose.
*
* @author Sebastiano Vigna
*/
public class SafeByteArrayOutputStream extends SafeMeasurableOutputStream implements SafeRepositionableStream {
/** The array backing the output stream. */
public static final int DEFAULT_INITIAL_CAPACITY = 16;
private final boolean wrapped;
private final int initialPosition;
private final int initialLength;
/** The array backing the output stream. */
public byte[] array;
/** The number of valid bytes in {@link #array}. */
public int length;
/** The current writing position. */
private int position;
/** Creates a new array output stream with an initial capacity of {@link #DEFAULT_INITIAL_CAPACITY} bytes. */
public SafeByteArrayOutputStream() {
this(DEFAULT_INITIAL_CAPACITY);
}
/** Creates a new array output stream with a given initial capacity.
*
* @param initialCapacity the initial length of the backing array.
*/
public SafeByteArrayOutputStream(final int initialCapacity) {
array = new byte[initialCapacity];
wrapped = false;
initialPosition = 0;
initialLength = 0;
}
/** Creates a new array output stream wrapping a given byte array.
*
* @param a the byte array to wrap.
*/
public SafeByteArrayOutputStream(final byte[] a) {
array = a;
wrapped = true;
initialPosition = 0;
initialLength = a.length;
}
/** Creates a new array output stream wrapping a given byte array.
*
* @param a the byte array to wrap.
*/
public SafeByteArrayOutputStream(final byte[] a, int from, int to) {
Arrays.ensureFromTo(a.length, from, to);
wrapped = true;
array = a;
initialPosition = from;
initialLength = to;
position = from;
length = to - from;
}
/** Marks this array output stream as empty. */
public void reset() {
length = initialLength;
position = initialPosition;
}
/** Ensures that the length of the backing array is equal to {@link #length}. */
public void trim() {
if (!wrapped) {
array = ByteArrays.trim(array, length);
}
}
public void ensureWritable(int size) {
growBy(size);
}
@Override
public void write(final int b) {
if (position >= array.length) {
if (wrapped) {
throw new ArrayIndexOutOfBoundsException(position);
} else {
array = ByteArrays.grow(array, position + 1, length);
}
}
array[position++] = (byte)b;
if (length < position) length = position;
}
@Override
public void write(final byte[] b, final int off, final int len) {
ByteArrays.ensureOffsetLength(b, off, len);
growBy(len);
System.arraycopy(b, off, array, position, len);
if (position + len > length) length = position += len;
}
private void growBy(int len) {
if (position + len > array.length) {
if (wrapped) {
throw new ArrayIndexOutOfBoundsException(position + len - 1);
} else {
array = ByteArrays.grow(array, position + len, position);
}
}
}
@Override
public void position(final long newPosition) {
position = (int)newPosition;
}
@Override
public long position() {
return position;
}
@Override
public long length() {
return length;
}
}

View File

@ -1,580 +0,0 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.warp.commonutils.stream;
import java.io.DataInput;
import org.jetbrains.annotations.NotNull;
/**
* A data input stream lets an application read primitive Java data
* types from an underlying input stream in a machine-independent
* way. An application uses a data output stream to write data that
* can later be read by a data input stream.
* <p>
* DataInputStream is not necessarily safe for multithreaded access.
* Thread safety is optional and is the responsibility of users of
* methods in this class.
*
* @author Arthur van Hoff
* @see java.io.DataOutputStream
* @since 1.0
*/
public class SafeDataInputStream extends SafeFilterInputStream implements DataInput {
/**
* Creates a DataInputStream that uses the specified
* underlying InputStream.
*
* @param in the specified input stream
*/
public SafeDataInputStream(SafeInputStream in) {
super(in);
}
/**
* working arrays initialized on demand by readUTF
*/
private byte[] bytearr = new byte[80];
private char[] chararr = new char[80];
/**
* Reads some number of bytes from the contained input stream and
* stores them into the buffer array {@code b}. The number of
* bytes actually read is returned as an integer. This method blocks
* until input data is available, end of file is detected, or an
* exception is thrown.
*
* <p>If {@code b} is null, a {@code NullPointerException} is
* thrown. If the length of {@code b} is zero, then no bytes are
* read and {@code 0} is returned; otherwise, there is an attempt
* to read at least one byte. If no byte is available because the
* stream is at end of file, the value {@code -1} is returned;
* otherwise, at least one byte is read and stored into {@code b}.
*
* <p>The first byte read is stored into element {@code b[0]}, the
* next one into {@code b[1]}, and so on. The number of bytes read
* is, at most, equal to the length of {@code b}. Let {@code k}
* be the number of bytes actually read; these bytes will be stored in
* elements {@code b[0]} through {@code b[k-1]}, leaving
* elements {@code b[k]} through {@code b[b.length-1]}
* unaffected.
*
* <p>The {@code read(b)} method has the same effect as:
* <blockquote><pre>
* read(b, 0, b.length)
* </pre></blockquote>
*
* @param b the buffer into which the data is read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end
* of the stream has been reached.
* @see SafeFilterInputStream#in
* @see java.io.InputStream#read(byte[], int, int)
*/
public final int read(byte[] b) {
return in.read(b, 0, b.length);
}
/**
* Reads up to {@code len} bytes of data from the contained
* input stream into an array of bytes. An attempt is made to read
* as many as {@code len} bytes, but a smaller number may be read,
* possibly zero. The number of bytes actually read is returned as an
* integer.
*
* <p> This method blocks until input data is available, end of file is
* detected, or an exception is thrown.
*
* <p> If {@code len} is zero, then no bytes are read and
* {@code 0} is returned; otherwise, there is an attempt to read at
* least one byte. If no byte is available because the stream is at end of
* file, the value {@code -1} is returned; otherwise, at least one
* byte is read and stored into {@code b}.
*
* <p> The first byte read is stored into element {@code b[off]}, the
* next one into {@code b[off+1]}, and so on. The number of bytes read
* is, at most, equal to {@code len}. Let <i>k</i> be the number of
* bytes actually read; these bytes will be stored in elements
* {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},
* leaving elements {@code b[off+}<i>k</i>{@code ]} through
* {@code b[off+len-1]} unaffected.
*
* <p> In every case, elements {@code b[0]} through
* {@code b[off]} and elements {@code b[off+len]} through
* {@code b[b.length-1]} are unaffected.
*
* @param b the buffer into which the data is read.
* @param off the start offset in the destination array {@code b}
* @param len the maximum number of bytes read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end
* of the stream has been reached.
* @throws NullPointerException If {@code b} is {@code null}.
* @throws IndexOutOfBoundsException If {@code off} is negative,
* {@code len} is negative, or {@code len} is greater than
* {@code b.length - off}
* @see SafeFilterInputStream#in
* @see java.io.InputStream#read(byte[], int, int)
*/
public final int read(byte[] b, int off, int len) {
return in.read(b, off, len);
}
/**
* See the general contract of the {@code readFully}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @param b the buffer into which the data is read.
* @throws NullPointerException if {@code b} is {@code null}.
* @see SafeFilterInputStream#in
*/
public final void readFully(byte @NotNull [] b) {
readFully(b, 0, b.length);
}
/**
* See the general contract of the {@code readFully}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @param b the buffer into which the data is read.
* @param off the start offset in the data array {@code b}.
* @param len the number of bytes to read.
* @throws NullPointerException if {@code b} is {@code null}.
* @throws IndexOutOfBoundsException if {@code off} is negative,
* {@code len} is negative, or {@code len} is greater than
* {@code b.length - off}.
* @see SafeFilterInputStream#in
*/
public final void readFully(byte @NotNull [] b, int off, int len) {
if (len < 0)
throw new IndexOutOfBoundsException();
int n = 0;
while (n < len) {
int count = in.read(b, off + n, len - n);
if (count < 0)
throw new IndexOutOfBoundsException();
n += count;
}
}
/**
* See the general contract of the {@code skipBytes}
* method of {@code DataInput}.
* <p>
* Bytes for this operation are read from the contained
* input stream.
*
* @param n the number of bytes to be skipped.
* @return the actual number of bytes skipped.
*/
public final int skipBytes(int n) {
int total = 0;
int cur;
while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
total += cur;
}
return total;
}
/**
* See the general contract of the {@code readBoolean}
* method of {@code DataInput}.
* <p>
* Bytes for this operation are read from the contained
* input stream.
*
* @return the {@code boolean} value read.
* @see SafeFilterInputStream#in
*/
public final boolean readBoolean() {
int ch = in.read();
if (ch < 0)
throw new IndexOutOfBoundsException();
return (ch != 0);
}
/**
* See the general contract of the {@code readByte}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next byte of this input stream as a signed 8-bit
* {@code byte}.
* @see SafeFilterInputStream#in
*/
public final byte readByte() {
int ch = in.read();
if (ch < 0)
throw new IndexOutOfBoundsException();
return (byte)(ch);
}
/**
* See the general contract of the {@code readUnsignedByte}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next byte of this input stream, interpreted as an
* unsigned 8-bit number.
* @see SafeFilterInputStream#in
*/
public final int readUnsignedByte() {
int ch = in.read();
if (ch < 0)
throw new IndexOutOfBoundsException();
return ch;
}
/**
* See the general contract of the {@code readShort}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next two bytes of this input stream, interpreted as a
* signed 16-bit number.
* @see SafeFilterInputStream#in
*/
public final short readShort() {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new IndexOutOfBoundsException();
return (short)((ch1 << 8) + (ch2 << 0));
}
/**
* See the general contract of the {@code readUnsignedShort}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next two bytes of this input stream, interpreted as an
* unsigned 16-bit integer.
* @see SafeFilterInputStream#in
*/
public final int readUnsignedShort() {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new IndexOutOfBoundsException();
return (ch1 << 8) + (ch2 << 0);
}
/**
* See the general contract of the {@code readChar}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next two bytes of this input stream, interpreted as a
* {@code char}.
* @see SafeFilterInputStream#in
*/
public final char readChar() {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new IndexOutOfBoundsException();
return (char)((ch1 << 8) + (ch2 << 0));
}
/**
* See the general contract of the {@code readInt}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next four bytes of this input stream, interpreted as an
* {@code int}.
* @see SafeFilterInputStream#in
*/
public final int readInt() {
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new IndexOutOfBoundsException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
private final byte[] readBuffer = new byte[8];
/**
* See the general contract of the {@code readLong}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next eight bytes of this input stream, interpreted as a
* {@code long}.
* @see SafeFilterInputStream#in
*/
public final long readLong() {
readFully(readBuffer, 0, 8);
return (((long)readBuffer[0] << 56) +
((long)(readBuffer[1] & 255) << 48) +
((long)(readBuffer[2] & 255) << 40) +
((long)(readBuffer[3] & 255) << 32) +
((long)(readBuffer[4] & 255) << 24) +
((readBuffer[5] & 255) << 16) +
((readBuffer[6] & 255) << 8) +
((readBuffer[7] & 255) << 0));
}
/**
* See the general contract of the {@code readFloat}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next four bytes of this input stream, interpreted as a
* {@code float}.
* @see SafeDataInputStream#readInt()
* @see Float#intBitsToFloat(int)
*/
public final float readFloat() {
return Float.intBitsToFloat(readInt());
}
/**
* See the general contract of the {@code readDouble}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next eight bytes of this input stream, interpreted as a
* {@code double}.
* @see SafeDataInputStream#readLong()
* @see Double#longBitsToDouble(long)
*/
public final double readDouble() {
return Double.longBitsToDouble(readLong());
}
private char[] lineBuffer;
/**
* See the general contract of the {@code readLine}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @deprecated This method does not properly convert bytes to characters.
* As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
* {@code BufferedReader.readLine()} method. Programs that use the
* {@code DataInputStream} class to read lines can be converted to use
* the {@code BufferedReader} class by replacing code of the form:
* <blockquote><pre>
* DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
* </pre></blockquote>
* with:
* <blockquote><pre>
* BufferedReader d
* =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
* </pre></blockquote>
*
* @return the next line of text from this input stream.
* @see java.io.BufferedReader#readLine()
* @see SafeFilterInputStream#in
*/
@Deprecated
public final String readLine() {
char[] buf = lineBuffer;
if (buf == null) {
buf = lineBuffer = new char[128];
}
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) {
if (!(in instanceof SafePushbackInputStream)) {
this.in = new SafePushbackInputStream(in);
}
((SafePushbackInputStream)in).unread(c2);
}
break loop;
default:
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) {
return null;
}
return String.copyValueOf(buf, 0, offset);
}
/**
* See the general contract of the {@code readUTF}
* method of {@code DataInput}.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return a Unicode string.
* @see SafeDataInputStream#readUTF(SafeDataInputStream)
*/
public final @NotNull String readUTF() {
return readUTF(this);
}
/**
* Reads from the
* stream {@code in} a representation
* of a Unicode character string encoded in
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
* this string of characters is then returned as a {@code String}.
* The details of the modified UTF-8 representation
* are exactly the same as for the {@code readUTF}
* method of {@code DataInput}.
*
* @param in a data input stream.
* @return a Unicode string.
* @see SafeDataInputStream#readUnsignedShort()
*/
public static String readUTF(SafeDataInputStream in) {
int utflen = in.readUnsignedShort();
byte[] bytearr;
char[] chararr;
if (in.bytearr.length < utflen){
in.bytearr = new byte[utflen*2];
in.chararr = new char[utflen*2];
}
chararr = in.chararr;
bytearr = in.bytearr;
int c, char2, char3;
int count = 0;
int chararr_count=0;
in.readFully(bytearr, 0, utflen);
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
if (c > 127) break;
count++;
chararr[chararr_count++]=(char)c;
}
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
/* 0xxxxxxx*/
count++;
chararr[chararr_count++]=(char)c;
break;
case 12: case 13:
/* 110x xxxx 10xx xxxx*/
count += 2;
if (count > utflen)
throw new IllegalArgumentException(
"malformed input: partial character at end");
char2 = bytearr[count-1];
if ((char2 & 0xC0) != 0x80)
throw new IllegalArgumentException(
"malformed input around byte " + count);
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
throw new IllegalArgumentException(
"malformed input: partial character at end");
char2 = bytearr[count-2];
char3 = bytearr[count-1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new IllegalArgumentException(
"malformed input around byte " + (count-1));
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
/* 10xx xxxx, 1111 xxxx */
throw new IllegalArgumentException(
"malformed input around byte " + count);
}
}
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
}
}

View File

@ -1,339 +0,0 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.warp.commonutils.stream;
/**
* The {@code SafeDataOutput} interface provides
* for converting data from any of the Java
* primitive types to a series of bytes and
* writing these bytes to a binary stream.
* There is also a facility for converting
* a {@code String} into
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
* format and writing the resulting series
* of bytes.
* <p>
* For all the methods in this interface that
* write bytes, it is generally true that if
* a byte cannot be written for any reason,
* an {@code IOException} is thrown.
*
* @author Frank Yellin
* @see java.io.DataInput
* @see java.io.DataOutputStream
* @since 1.0
*/
public interface SafeDataOutput {
/**
* Writes to the output stream the eight
* low-order bits of the argument {@code b}.
* The 24 high-order bits of {@code b}
* are ignored.
*
* @param b the byte to be written.
*/
void write(int b);
/**
* Writes to the output stream all the bytes in array {@code b}.
* If {@code b} is {@code null},
* a {@code NullPointerException} is thrown.
* If {@code b.length} is zero, then
* no bytes are written. Otherwise, the byte
* {@code b[0]} is written first, then
* {@code b[1]}, and so on; the last byte
* written is {@code b[b.length-1]}.
*
* @param b the data.
*/
void write(byte b[]);
/**
* Writes {@code len} bytes from array
* {@code b}, in order, to
* the output stream. If {@code b}
* is {@code null}, a {@code NullPointerException}
* is thrown. If {@code off} is negative,
* or {@code len} is negative, or {@code off+len}
* is greater than the length of the array
* {@code b}, then an {@code IndexOutOfBoundsException}
* is thrown. If {@code len} is zero,
* then no bytes are written. Otherwise, the
* byte {@code b[off]} is written first,
* then {@code b[off+1]}, and so on; the
* last byte written is {@code b[off+len-1]}.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
*/
void write(byte b[], int off, int len);
/**
* Writes a {@code boolean} value to this output stream.
* If the argument {@code v}
* is {@code true}, the value {@code (byte)1}
* is written; if {@code v} is {@code false},
* the value {@code (byte)0} is written.
* The byte written by this method may
* be read by the {@code readBoolean}
* method of interface {@code DataInput},
* which will then return a {@code boolean}
* equal to {@code v}.
*
* @param v the boolean to be written.
*/
void writeBoolean(boolean v);
/**
* Writes to the output stream the eight low-order
* bits of the argument {@code v}.
* The 24 high-order bits of {@code v}
* are ignored. (This means that {@code writeByte}
* does exactly the same thing as {@code write}
* for an integer argument.) The byte written
* by this method may be read by the {@code readByte}
* method of interface {@code DataInput},
* which will then return a {@code byte}
* equal to {@code (byte)v}.
*
* @param v the byte value to be written.
*/
void writeByte(int v);
/**
* Writes two bytes to the output
* stream to represent the value of the argument.
* The byte values to be written, in the order
* shown, are:
* <pre>{@code
* (byte)(0xff & (v >> 8))
* (byte)(0xff & v)
* }</pre> <p>
* The bytes written by this method may be
* read by the {@code readShort} method
* of interface {@code DataInput}, which
* will then return a {@code short} equal
* to {@code (short)v}.
*
* @param v the {@code short} value to be written.
*/
void writeShort(int v);
/**
* Writes a {@code char} value, which
* is comprised of two bytes, to the
* output stream.
* The byte values to be written, in the order
* shown, are:
* <pre>{@code
* (byte)(0xff & (v >> 8))
* (byte)(0xff & v)
* }</pre><p>
* The bytes written by this method may be
* read by the {@code readChar} method
* of interface {@code DataInput}, which
* will then return a {@code char} equal
* to {@code (char)v}.
*
* @param v the {@code char} value to be written.
*/
void writeChar(int v);
/**
* Writes an {@code int} value, which is
* comprised of four bytes, to the output stream.
* The byte values to be written, in the order
* shown, are:
* <pre>{@code
* (byte)(0xff & (v >> 24))
* (byte)(0xff & (v >> 16))
* (byte)(0xff & (v >> 8))
* (byte)(0xff & v)
* }</pre><p>
* The bytes written by this method may be read
* by the {@code readInt} method of interface
* {@code DataInput}, which will then
* return an {@code int} equal to {@code v}.
*
* @param v the {@code int} value to be written.
*/
void writeInt(int v);
/**
* Writes a {@code long} value, which is
* comprised of eight bytes, to the output stream.
* The byte values to be written, in the order
* shown, are:
* <pre>{@code
* (byte)(0xff & (v >> 56))
* (byte)(0xff & (v >> 48))
* (byte)(0xff & (v >> 40))
* (byte)(0xff & (v >> 32))
* (byte)(0xff & (v >> 24))
* (byte)(0xff & (v >> 16))
* (byte)(0xff & (v >> 8))
* (byte)(0xff & v)
* }</pre><p>
* The bytes written by this method may be
* read by the {@code readLong} method
* of interface {@code DataInput}, which
* will then return a {@code long} equal
* to {@code v}.
*
* @param v the {@code long} value to be written.
*/
void writeLong(long v);
/**
* Writes a {@code float} value,
* which is comprised of four bytes, to the output stream.
* It does this as if it first converts this
* {@code float} value to an {@code int}
* in exactly the manner of the {@code Float.floatToIntBits}
* method and then writes the {@code int}
* value in exactly the manner of the {@code writeInt}
* method. The bytes written by this method
* may be read by the {@code readFloat}
* method of interface {@code DataInput},
* which will then return a {@code float}
* equal to {@code v}.
*
* @param v the {@code float} value to be written.
*/
void writeFloat(float v);
/**
* Writes a {@code double} value,
* which is comprised of eight bytes, to the output stream.
* It does this as if it first converts this
* {@code double} value to a {@code long}
* in exactly the manner of the {@code Double.doubleToLongBits}
* method and then writes the {@code long}
* value in exactly the manner of the {@code writeLong}
* method. The bytes written by this method
* may be read by the {@code readDouble}
* method of interface {@code DataInput},
* which will then return a {@code double}
* equal to {@code v}.
*
* @param v the {@code double} value to be written.
*/
void writeDouble(double v);
/**
* Writes a string to the output stream.
* For every character in the string
* {@code s}, taken in order, one byte
* is written to the output stream. If
* {@code s} is {@code null}, a {@code NullPointerException}
* is thrown.<p> If {@code s.length}
* is zero, then no bytes are written. Otherwise,
* the character {@code s[0]} is written
* first, then {@code s[1]}, and so on;
* the last character written is {@code s[s.length-1]}.
* For each character, one byte is written,
* the low-order byte, in exactly the manner
* of the {@code writeByte} method . The
* high-order eight bits of each character
* in the string are ignored.
*
* @param s the string of bytes to be written.
*/
void writeBytes(String s);
/**
* Writes every character in the string {@code s},
* to the output stream, in order,
* two bytes per character. If {@code s}
* is {@code null}, a {@code NullPointerException}
* is thrown. If {@code s.length}
* is zero, then no characters are written.
* Otherwise, the character {@code s[0]}
* is written first, then {@code s[1]},
* and so on; the last character written is
* {@code s[s.length-1]}. For each character,
* two bytes are actually written, high-order
* byte first, in exactly the manner of the
* {@code writeChar} method.
*
* @param s the string value to be written.
*/
void writeChars(String s);
/**
* Writes two bytes of length information
* to the output stream, followed
* by the
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
* representation
* of every character in the string {@code s}.
* If {@code s} is {@code null},
* a {@code NullPointerException} is thrown.
* Each character in the string {@code s}
* is converted to a group of one, two, or
* three bytes, depending on the value of the
* character.<p>
* If a character {@code c}
* is in the range <code>&#92;u0001</code> through
* <code>&#92;u007f</code>, it is represented
* by one byte:
* <pre>(byte)c </pre> <p>
* If a character {@code c} is <code>&#92;u0000</code>
* or is in the range <code>&#92;u0080</code>
* through <code>&#92;u07ff</code>, then it is
* represented by two bytes, to be written
* in the order shown: <pre>{@code
* (byte)(0xc0 | (0x1f & (c >> 6)))
* (byte)(0x80 | (0x3f & c))
* }</pre> <p> If a character
* {@code c} is in the range <code>&#92;u0800</code>
* through {@code uffff}, then it is
* represented by three bytes, to be written
* in the order shown: <pre>{@code
* (byte)(0xe0 | (0x0f & (c >> 12)))
* (byte)(0x80 | (0x3f & (c >> 6)))
* (byte)(0x80 | (0x3f & c))
* }</pre> <p> First,
* the total number of bytes needed to represent
* all the characters of {@code s} is
* calculated. If this number is larger than
* {@code 65535}, then a {@code UTFDataFormatException}
* is thrown. Otherwise, this length is written
* to the output stream in exactly the manner
* of the {@code writeShort} method;
* after this, the one-, two-, or three-byte
* representation of each character in the
* string {@code s} is written.<p> The
* bytes written by this method may be read
* by the {@code readUTF} method of interface
* {@code DataInput}, which will then
* return a {@code String} equal to {@code s}.
*
* @param s the string value to be written.
*/
void writeUTF(String s);
}

View File

@ -1,423 +0,0 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.warp.commonutils.stream;
import java.io.DataOutputStream;
/**
* A data output stream lets an application write primitive Java data
* types to an output stream in a portable way. An application can
* then use a data input stream to read the data back in.
*
* @author unascribed
* @see java.io.DataInputStream
* @since 1.0
*/
public class SafeDataOutputStream extends SafeFilterOutputStream implements SafeDataOutput {
/**
* The number of bytes written to the data output stream so far.
* If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
*/
protected int written;
/**
* bytearr is initialized on demand by writeUTF
*/
private byte[] bytearr = null;
/**
* Creates a new data output stream to write data to the specified
* underlying output stream. The counter {@code written} is
* set to zero.
*
* @param out the underlying output stream, to be saved for later
* use.
* @see SafeFilterOutputStream#out
*/
public SafeDataOutputStream(SafeOutputStream out) {
super(out);
}
/**
* Increases the written counter by the specified value
* until it reaches Integer.MAX_VALUE.
*/
private void incCount(int value) {
int temp = written + value;
if (temp < 0) {
temp = Integer.MAX_VALUE;
}
written = temp;
}
/**
* Writes the specified byte (the low eight bits of the argument
* {@code b}) to the underlying output stream. If no exception
* is thrown, the counter {@code written} is incremented by
* {@code 1}.
* <p>
* Implements the {@code write} method of {@code OutputStream}.
*
* @param b the {@code byte} to be written.
* @see SafeFilterOutputStream#out
*/
public void write(int b) {
out.write(b);
incCount(1);
}
/**
* Writes {@code len} bytes from the specified byte array
* starting at offset {@code off} to the underlying output stream.
* If no exception is thrown, the counter {@code written} is
* incremented by {@code len}.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @see SafeFilterOutputStream#out
*/
public void write(byte[] b, int off, int len)
{
out.write(b, off, len);
incCount(len);
}
/**
* Flushes this data output stream. This forces any buffered output
* bytes to be written out to the stream.
* <p>
* The {@code flush} method of {@code SafeDataOutputStream}
* calls the {@code flush} method of its underlying output stream.
*
* @see SafeFilterOutputStream#out
* @see java.io.OutputStream#flush()
*/
public void flush() {
out.flush();
}
/**
* Writes a {@code boolean} to the underlying output stream as
* a 1-byte value. The value {@code true} is written out as the
* value {@code (byte)1}; the value {@code false} is
* written out as the value {@code (byte)0}. If no exception is
* thrown, the counter {@code written} is incremented by
* {@code 1}.
*
* @param v a {@code boolean} value to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeBoolean(boolean v) {
out.write(v ? 1 : 0);
incCount(1);
}
/**
* Writes out a {@code byte} to the underlying output stream as
* a 1-byte value. If no exception is thrown, the counter
* {@code written} is incremented by {@code 1}.
*
* @param v a {@code byte} value to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeByte(int v) {
out.write(v);
incCount(1);
}
/**
* Writes a {@code short} to the underlying output stream as two
* bytes, high byte first. If no exception is thrown, the counter
* {@code written} is incremented by {@code 2}.
*
* @param v a {@code short} to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeShort(int v) {
out.write((v >>> 8) & 0xFF);
out.write((v) & 0xFF);
incCount(2);
}
/**
* Writes a {@code char} to the underlying output stream as a
* 2-byte value, high byte first. If no exception is thrown, the
* counter {@code written} is incremented by {@code 2}.
*
* @param v a {@code char} value to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeChar(int v) {
out.write((v >>> 8) & 0xFF);
out.write((v) & 0xFF);
incCount(2);
}
/**
* Writes an {@code int} to the underlying output stream as four
* bytes, high byte first. If no exception is thrown, the counter
* {@code written} is incremented by {@code 4}.
*
* @param v an {@code int} to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeInt(int v) {
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v) & 0xFF);
incCount(4);
}
private final byte[] writeBuffer = new byte[8];
/**
* Writes a {@code long} to the underlying output stream as eight
* bytes, high byte first. In no exception is thrown, the counter
* {@code written} is incremented by {@code 8}.
*
* @param v a {@code long} to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeLong(long v) {
writeBuffer[0] = (byte)(v >>> 56);
writeBuffer[1] = (byte)(v >>> 48);
writeBuffer[2] = (byte)(v >>> 40);
writeBuffer[3] = (byte)(v >>> 32);
writeBuffer[4] = (byte)(v >>> 24);
writeBuffer[5] = (byte)(v >>> 16);
writeBuffer[6] = (byte)(v >>> 8);
writeBuffer[7] = (byte)(v);
out.write(writeBuffer, 0, 8);
incCount(8);
}
/**
* Converts the float argument to an {@code int} using the
* {@code floatToIntBits} method in class {@code Float},
* and then writes that {@code int} value to the underlying
* output stream as a 4-byte quantity, high byte first. If no
* exception is thrown, the counter {@code written} is
* incremented by {@code 4}.
*
* @param v a {@code float} value to be written.
* @see SafeFilterOutputStream#out
* @see Float#floatToIntBits(float)
*/
public final void writeFloat(float v) {
writeInt(Float.floatToIntBits(v));
}
/**
* Converts the double argument to a {@code long} using the
* {@code doubleToLongBits} method in class {@code Double},
* and then writes that {@code long} value to the underlying
* output stream as an 8-byte quantity, high byte first. If no
* exception is thrown, the counter {@code written} is
* incremented by {@code 8}.
*
* @param v a {@code double} value to be written.
* @see SafeFilterOutputStream#out
* @see Double#doubleToLongBits(double)
*/
public final void writeDouble(double v) {
writeLong(Double.doubleToLongBits(v));
}
/**
* Writes out the string to the underlying output stream as a
* sequence of bytes. Each character in the string is written out, in
* sequence, by discarding its high eight bits. If no exception is
* thrown, the counter {@code written} is incremented by the
* length of {@code s}.
*
* @param s a string of bytes to be written.
* @see SafeFilterOutputStream#out
*/
public final void writeBytes(String s) {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
out.write((byte)s.charAt(i));
}
incCount(len);
}
/**
* Writes a string to the underlying output stream as a sequence of
* characters. Each character is written to the data output stream as
* if by the {@code writeChar} method. If no exception is
* thrown, the counter {@code written} is incremented by twice
* the length of {@code s}.
*
* @param s a {@code String} value to be written.
* @see SafeDataOutputStream#writeChar(int)
* @see SafeFilterOutputStream#out
*/
public final void writeChars(String s) {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
int v = s.charAt(i);
out.write((v >>> 8) & 0xFF);
out.write((v) & 0xFF);
}
incCount(len * 2);
}
/**
* Writes a string to the underlying output stream using
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
* encoding in a machine-independent manner.
* <p>
* First, two bytes are written to the output stream as if by the
* {@code writeShort} method giving the number of bytes to
* follow. This value is the number of bytes actually written out,
* not the length of the string. Following the length, each character
* of the string is output, in sequence, using the modified UTF-8 encoding
* for the character. If no exception is thrown, the counter
* {@code written} is incremented by the total number of
* bytes written to the output stream. This will be at least two
* plus the length of {@code str}, and at most two plus
* thrice the length of {@code str}.
*
* @param str a string to be written.
* @see #writeChars(String)
*/
public final void writeUTF(String str) {
writeUTF(str, this);
}
public final void writeUTF(int strlen, int utflen, String str) {
writeUTF(strlen, utflen, str, this);
}
/**
* Writes a string to the specified DataOutput using
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
* encoding in a machine-independent manner.
* <p>
* First, two bytes are written to out as if by the {@code writeShort}
* method giving the number of bytes to follow. This value is the number of
* bytes actually written out, not the length of the string. Following the
* length, each character of the string is output, in sequence, using the
* modified UTF-8 encoding for the character. If no exception is thrown, the
* counter {@code written} is incremented by the total number of
* bytes written to the output stream. This will be at least two
* plus the length of {@code str}, and at most two plus
* thrice the length of {@code str}.
*
* @param str a string to be written.
* @param out destination to write to
* @return The number of bytes written out.
*/
static int writeUTF(String str, SafeDataOutput out) {
int strlen = strLen(str);
int utflen = utfLen(str, strlen);
return writeUTF(strlen, utflen, str, out);
}
public static int strLen(String str) {
return str.length();
}
public static int utfLen(String str, int strLen) {
int utflen = strLen; // optimized for ASCII
for (int i = 0; i < strLen; i++) {
int c = str.charAt(i);
if (c >= 0x80 || c == 0)
utflen += (c >= 0x800) ? 2 : 1;
}
if (utflen > 65535 || /* overflow */ utflen < strLen)
throw new IllegalArgumentException(tooLongMsg(str, utflen));
return utflen;
}
static int writeUTF(int strlen, int utflen, String str, SafeDataOutput out) {
final byte[] bytearr;
if (out instanceof SafeDataOutputStream dos) {
if (dos.bytearr == null || (dos.bytearr.length < (utflen + 2)))
dos.bytearr = new byte[(utflen*2) + 2];
bytearr = dos.bytearr;
} else {
bytearr = new byte[utflen + 2];
}
int count = 0;
bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
bytearr[count++] = (byte) ((utflen) & 0xFF);
int i;
for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII
int c = str.charAt(i);
if (c >= 0x80 || c == 0) break;
bytearr[count++] = (byte) c;
}
for (; i < strlen; i++) {
int c = str.charAt(i);
if (c < 0x80 && c != 0) {
bytearr[count++] = (byte) c;
} else if (c >= 0x800) {
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c) & 0x3F));
} else {
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c) & 0x3F));
}
}
out.write(bytearr, 0, utflen + 2);
return utflen + 2;
}
private static String tooLongMsg(String s, int bits32) {
int slen = s.length();
String head = s.substring(0, 8);
String tail = s.substring(slen - 8, slen);
// handle int overflow with max 3x expansion
long actualLength = (long)slen + Integer.toUnsignedLong(bits32 - slen);
return "encoded string (" + head + "..." + tail + ") too long: "
+ actualLength + " bytes";
}
/**
* Returns the current value of the counter {@code written},
* the number of bytes written to this data output stream so far.
* If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
*
* @return the value of the {@code written} field.
* @see SafeDataOutputStream#written
*/
public final int size() {
return written;
}
public DataOutputStream asDataOutputStream() {
return new DataOutputStream(this.out);
}
}

View File

@ -1,210 +0,0 @@
package org.warp.commonutils.stream;
/**
* A {@code FilterInputStream} contains
* some other input stream, which it uses as
* its basic source of data, possibly transforming
* the data along the way or providing additional
* functionality. The class {@code FilterInputStream}
* itself simply overrides all methods of
* {@code InputStream} with versions that
* pass all requests to the contained input
* stream. Subclasses of {@code FilterInputStream}
* may further override some of these methods
* and may also provide additional methods
* and fields.
*
* @author Jonathan Payne
* @since 1.0
*/
public class SafeFilterInputStream extends SafeInputStream {
/**
* The input stream to be filtered.
*/
protected volatile SafeInputStream in;
/**
* Creates a {@code FilterInputStream}
* by assigning the argument {@code in}
* to the field {@code this.in} so as
* to remember it for later use.
*
* @param in the underlying input stream, or {@code null} if
* this instance is to be created without an underlying stream.
*/
protected SafeFilterInputStream(SafeInputStream in) {
this.in = in;
}
/**
* Reads the next byte of data from this input stream. The value
* byte is returned as an {@code int} in the range
* {@code 0} to {@code 255}. If no byte is available
* because the end of the stream has been reached, the value
* {@code -1} is returned. This method blocks until input data
* is available, the end of the stream is detected, or an exception
* is thrown.
* <p>
* This method
* simply performs {@code in.read()} and returns the result.
*
* @return the next byte of data, or {@code -1} if the end of the
* stream is reached.
* @see SafeFilterInputStream#in
*/
public int read() {
return in.read();
}
/**
* Reads up to {@code b.length} bytes of data from this
* input stream into an array of bytes. This method blocks until some
* input is available.
* <p>
* This method simply performs the call
* {@code read(b, 0, b.length)} and returns
* the result. It is important that it does
* <i>not</i> do {@code in.read(b)} instead;
* certain subclasses of {@code FilterInputStream}
* depend on the implementation strategy actually
* used.
*
* @param b the buffer into which the data is read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end of
* the stream has been reached.
* @see SafeFilterInputStream#read(byte[], int, int)
*/
public int read(byte b[]) {
return read(b, 0, b.length);
}
/**
* Reads up to {@code len} bytes of data from this input stream
* into an array of bytes. If {@code len} is not zero, the method
* blocks until some input is available; otherwise, no
* bytes are read and {@code 0} is returned.
* <p>
* This method simply performs {@code in.read(b, off, len)}
* and returns the result.
*
* @param b the buffer into which the data is read.
* @param off the start offset in the destination array {@code b}
* @param len the maximum number of bytes read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end of
* the stream has been reached.
* @throws NullPointerException If {@code b} is {@code null}.
* @throws IndexOutOfBoundsException If {@code off} is negative,
* {@code len} is negative, or {@code len} is greater than
* {@code b.length - off}
* @see SafeFilterInputStream#in
*/
public int read(byte b[], int off, int len) {
return in.read(b, off, len);
}
/**
* Skips over and discards {@code n} bytes of data from the
* input stream. The {@code skip} method may, for a variety of
* reasons, end up skipping over some smaller number of bytes,
* possibly {@code 0}. The actual number of bytes skipped is
* returned.
* <p>
* This method simply performs {@code in.skip(n)}.
*
* @param n the number of bytes to be skipped.
* @return the actual number of bytes skipped.
*/
public long skip(long n) {
return in.skip(n);
}
/**
* Returns an estimate of the number of bytes that can be read (or
* skipped over) from this input stream without blocking by the next
* caller of a method for this input stream. The next caller might be
* the same thread or another thread. A single read or skip of this
* many bytes will not block, but may read or skip fewer bytes.
* <p>
* This method returns the result of {@link #in in}.available().
*
* @return an estimate of the number of bytes that can be read (or skipped
* over) from this input stream without blocking.
*/
public int available() {
return in.available();
}
/**
* Closes this input stream and releases any system resources
* associated with the stream.
* This
* method simply performs {@code in.close()}.
*
* @see SafeFilterInputStream#in
*/
public void close() {
in.close();
}
/**
* Marks the current position in this input stream. A subsequent
* call to the {@code reset} method repositions this stream at
* the last marked position so that subsequent reads re-read the same bytes.
* <p>
* The {@code readlimit} argument tells this input stream to
* allow that many bytes to be read before the mark position gets
* invalidated.
* <p>
* This method simply performs {@code in.mark(readlimit)}.
*
* @param readlimit the maximum limit of bytes that can be read before
* the mark position becomes invalid.
* @see SafeFilterInputStream#in
* @see SafeFilterInputStream#reset()
*/
public void mark(int readlimit) {
in.mark(readlimit);
}
/**
* Repositions this stream to the position at the time the
* {@code mark} method was last called on this input stream.
* <p>
* This method
* simply performs {@code in.reset()}.
* <p>
* Stream marks are intended to be used in
* situations where you need to read ahead a little to see what's in
* the stream. Often this is most easily done by invoking some
* general parser. If the stream is of the type handled by the
* parse, it just chugs along happily. If the stream is not of
* that type, the parser should toss an exception when it fails.
* If this happens within readlimit bytes, it allows the outer
* code to reset the stream and try another parser.
*
* @see SafeFilterInputStream#in
* @see SafeFilterInputStream#mark(int)
*/
public void reset() {
in.reset();
}
/**
* Tests if this input stream supports the {@code mark}
* and {@code reset} methods.
* This method
* simply performs {@code in.markSupported()}.
*
* @return {@code true} if this stream type supports the
* {@code mark} and {@code reset} method;
* {@code false} otherwise.
* @see SafeFilterInputStream#in
* @see java.io.InputStream#mark(int)
* @see java.io.InputStream#reset()
*/
public boolean markSupported() {
return in.markSupported();
}
}

View File

@ -1,180 +0,0 @@
package org.warp.commonutils.stream;
/**
* This class is the superclass of all classes that filter output
* streams. These streams sit on top of an already existing output
* stream (the <i>underlying</i> output stream) which it uses as its
* basic sink of data, but possibly transforming the data along the
* way or providing additional functionality.
* <p>
* The class {@code FilterOutputStream} itself simply overrides
* all methods of {@code SafeOutputStream} with versions that pass
* all requests to the underlying output stream. Subclasses of
* {@code FilterOutputStream} may further override some of these
* methods as well as provide additional methods and fields.
*
* @author Jonathan Payne
* @since 1.0
*/
public class SafeFilterOutputStream extends SafeOutputStream {
/**
* The underlying output stream to be filtered.
*/
protected SafeOutputStream out;
/**
* Whether the stream is closed; implicitly initialized to false.
*/
private volatile boolean closed;
/**
* Object used to prevent a race on the 'closed' instance variable.
*/
private final Object closeLock = new Object();
/**
* Creates an output stream filter built on top of the specified
* underlying output stream.
*
* @param out the underlying output stream to be assigned to
* the field {@code this.out} for later use, or
* {@code null} if this instance is to be
* created without an underlying stream.
*/
public SafeFilterOutputStream(SafeOutputStream out) {
this.out = out;
}
/**
* Writes the specified {@code byte} to this output stream.
* <p>
* The {@code write} method of {@code FilterOutputStream}
* calls the {@code write} method of its underlying output stream,
* that is, it performs {@code out.write(b)}.
* <p>
* Implements the abstract {@code write} method of {@code SafeOutputStream}.
*
* @param b the {@code byte}.
*/
@Override
public void write(int b) {
out.write(b);
}
/**
* Writes {@code b.length} bytes to this output stream.
* <p>
* The {@code write} method of {@code FilterOutputStream}
* calls its {@code write} method of three arguments with the
* arguments {@code b}, {@code 0}, and
* {@code b.length}.
* <p>
* Note that this method does not call the one-argument
* {@code write} method of its underlying output stream with
* the single argument {@code b}.
*
* @param b the data to be written.
* @see SafeFilterOutputStream#write(byte[], int, int)
*/
@Override
public void write(byte b[]) {
write(b, 0, b.length);
}
/**
* Writes {@code len} bytes from the specified
* {@code byte} array starting at offset {@code off} to
* this output stream.
* <p>
* The {@code write} method of {@code FilterOutputStream}
* calls the {@code write} method of one argument on each
* {@code byte} to output.
* <p>
* Note that this method does not call the {@code write} method
* of its underlying output stream with the same arguments. Subclasses
* of {@code FilterOutputStream} should provide a more efficient
* implementation of this method.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @see SafeFilterOutputStream#write(int)
*/
@Override
public void write(byte b[], int off, int len) {
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
/**
* Flushes this output stream and forces any buffered output bytes
* to be written out to the stream.
* <p>
* The {@code flush} method of {@code FilterOutputStream}
* calls the {@code flush} method of its underlying output stream.
*
* @see SafeFilterOutputStream#out
*/
@Override
public void flush() {
out.flush();
}
/**
* Closes this output stream and releases any system resources
* associated with the stream.
* <p>
* When not already closed, the {@code close} method of {@code
* FilterOutputStream} calls its {@code flush} method, and then
* calls the {@code close} method of its underlying output stream.
*
* @see SafeFilterOutputStream#flush()
* @see SafeFilterOutputStream#out
*/
@Override
public void close() {
if (closed) {
return;
}
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
Throwable flushException = null;
try {
flush();
} catch (Throwable e) {
flushException = e;
throw e;
} finally {
if (flushException == null) {
out.close();
} else {
try {
out.close();
} catch (Throwable closeException) {
// evaluate possible precedence of flushException over closeException
if ((flushException instanceof ThreadDeath) &&
!(closeException instanceof ThreadDeath)) {
flushException.addSuppressed(closeException);
throw (ThreadDeath) flushException;
}
if (flushException != closeException) {
closeException.addSuppressed(flushException);
}
throw closeException;
}
}
}
}
}

View File

@ -1,201 +0,0 @@
package org.warp.commonutils.stream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public abstract class SafeInputStream extends InputStream {
// MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
// use when skipping.
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
private static final int DEFAULT_BUFFER_SIZE = 8192;
@Override
public abstract int read();
public int read(byte b[]) {
return read(b, 0, b.length);
}
public int read(byte b[], int off, int len) {
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
return i;
}
public byte[] readAllBytes() {
return readNBytes(Integer.MAX_VALUE);
}
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
public byte[] readNBytes(int len) {
if (len < 0) {
throw new IllegalArgumentException("len < 0");
}
List<byte[]> bufs = null;
byte[] result = null;
int total = 0;
int remaining = len;
int n;
do {
byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
int nread = 0;
// read to EOF which may read more or less than buffer size
while ((n = read(buf, nread,
Math.min(buf.length - nread, remaining))) > 0) {
nread += n;
remaining -= n;
}
if (nread > 0) {
if (MAX_BUFFER_SIZE - total < nread) {
throw new OutOfMemoryError("Required array size too large");
}
total += nread;
if (result == null) {
result = buf;
} else {
if (bufs == null) {
bufs = new ArrayList<>();
bufs.add(result);
}
bufs.add(buf);
}
}
// if the last call to read returned -1 or the number of bytes
// requested have been read then break
} while (n >= 0 && remaining > 0);
if (bufs == null) {
if (result == null) {
return new byte[0];
}
return result.length == total ?
result : Arrays.copyOf(result, total);
}
result = new byte[total];
int offset = 0;
remaining = total;
for (byte[] b : bufs) {
int count = Math.min(b.length, remaining);
System.arraycopy(b, 0, result, offset, count);
offset += count;
remaining -= count;
}
return result;
}
public int readNBytes(byte[] b, int off, int len) {
Objects.checkFromIndexSize(off, len, b.length);
int n = 0;
while (n < len) {
int count = read(b, off + n, len - n);
if (count < 0)
break;
n += count;
}
return n;
}
public long skip(long n) {
long remaining = n;
int nr;
if (n <= 0) {
return 0;
}
int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
byte[] skipBuffer = new byte[size];
while (remaining > 0) {
nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
public void skipNBytes(long n) {
if (n > 0) {
long ns = skip(n);
if (ns >= 0 && ns < n) { // skipped too few bytes
// adjust number to skip
n -= ns;
// read until requested number skipped or EOS reached
while (n > 0 && read() != -1) {
n--;
}
// if not enough skipped, then EOFE
if (n != 0) {
throw new IndexOutOfBoundsException();
}
} else if (ns != n) { // skipped negative or too many bytes
throw new IllegalArgumentException("Unable to skip exactly");
}
}
}
public int available() {
return 0;
}
public void close() {}
public void mark(int readlimit) {}
public void reset() {
throw new UnsupportedOperationException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
public long transferTo(OutputStream out) {
Objects.requireNonNull(out, "out");
long transferred = 0;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int read;
while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
try {
out.write(buffer, 0, read);
} catch (IOException e) {
throw new IllegalStateException(e);
}
transferred += read;
}
return transferred;
}
}

View File

@ -1,28 +0,0 @@
package org.warp.commonutils.stream;
/*
* Copyright (C) 2005-2020 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.InputStream;
/** An {@link InputStream} that implements also the {@link SafeMeasurableStream} interface.
*
* @since 5.0.4
*/
public abstract class SafeMeasurableInputStream extends SafeInputStream implements SafeMeasurableStream {
}

View File

@ -1,27 +0,0 @@
package org.warp.commonutils.stream;
/*
* Copyright (C) 2005-2020 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.OutputStream;
/** An {@link OutputStream} that implements also the {@link SafeMeasurableStream} interface.
*
* @since 6.0.0
*/
public abstract class SafeMeasurableOutputStream extends SafeOutputStream implements SafeMeasurableStream {
}

View File

@ -1,53 +0,0 @@
package org.warp.commonutils.stream;
/*
* Copyright (C) 2005-2020 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import it.unimi.dsi.fastutil.io.FastBufferedInputStream;
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream;
import it.unimi.dsi.fastutil.io.MeasurableInputStream;
/** An stream that provides eager access to its length,
* and keeps track of the current position (e.g., the number of bytes read so far, or the current
* position of the file pointer).
*
* <p>This class has two methods, both specified as optional. This apparently bizarre
* behaviour is necessary because of wrapper classes which use reflection
* to support those methods (see, e.g., {@link MeasurableInputStream}, {@link FastBufferedInputStream} and {@link FastBufferedOutputStream}).
*
* @since 6.0.0
*/
public interface SafeMeasurableStream {
/** Returns the overall length of this stream (optional operation). In most cases, this will require the
* stream to perform some extra action, possibly changing the state of the input stream itself (typically, reading
* all the bytes up to the end, or flushing on output stream).
* Implementing classes should always document what state will the input stream be in
* after calling this method, and which kind of exception could be thrown.
*/
long length();
/** Returns the current position in this stream (optional operation).
*
* <p>Usually, the position is just the number of bytes read or written
* since the stream was opened, but in the case of a
* {@link it.unimi.dsi.fastutil.io.RepositionableStream} it
* represent the current position.
*/
long position();
}

Some files were not shown because too many files have changed in this diff Show More