Skip to content

Commit

Permalink
refactor: Remove dead code in ByteBufferInputStream (#5742)
Browse files Browse the repository at this point in the history
Reusing this code for the JS implementation of flight/barrage, and
noticed some dead/unused code.

Partial #188
  • Loading branch information
niloc132 authored Jul 11, 2024
1 parent d7afa8e commit b13af92
Showing 1 changed file with 9 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,27 @@
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import io.deephaven.base.string.cache.CharSequenceAdapterBuilder;
import io.deephaven.base.string.cache.StringCache;
import org.jetbrains.annotations.NotNull;

/**
* This is an InputStream implementation which reads from a java.nio.ByteBuffer. If a read operation crosses the end of
* the buffer, the BufferUnderflowException is converted to an EOFException.
*
* The stream contains no state other than that in in the buffer itself, so the buffer can be exchanged at will with the
* <p>
* The stream contains no state other than that in the buffer itself, so the buffer can be exchanged at will with the
* setBuffer() method.
* <p>
* Endianness is determined by the provided buffer itself.
*/
public class ByteBufferInputStream extends java.io.InputStream implements DataInput {

/** the buffer from which we read */
protected ByteBuffer buf;

private char[] utfChars;

/**
* The DataOutput interface always writes bytes in big-endian order, while ByteBuffer allows the order to be big- or
* little-endian. Set this flag true to assume that the buffer is bid-endian, or false to check the buffer's order
* at each write.
*/
// protected static final boolean ASSUME_BIG_ENDIAN = true;

/**
* Construct a new stream which reads from a byte buffer/
*/
public ByteBufferInputStream(ByteBuffer buf) {
this.buf = buf;
this.utfChars = new char[0];
}

/**
Expand All @@ -64,7 +54,7 @@ public int read() throws IOException {
}

@Override
public int read(byte b[]) throws IOException {
public int read(byte[] b) throws IOException {
int n = Math.min(buf.remaining(), b.length);
if (n == 0 && b.length > 0) {
return -1;
Expand All @@ -74,7 +64,7 @@ public int read(byte b[]) throws IOException {
}

@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException {
int n = Math.min(buf.remaining(), len);
if (n == 0 && len > 0) {
return -1;
Expand Down Expand Up @@ -123,7 +113,7 @@ public boolean markSupported() {
// -----------------------------------------------------------------------------------

@Override
public void readFully(byte b[]) throws IOException {
public void readFully(byte[] b) throws IOException {
try {
buf.get(b, 0, b.length);
} catch (BufferUnderflowException x) {
Expand All @@ -132,7 +122,7 @@ public void readFully(byte b[]) throws IOException {
}

@Override
public void readFully(byte b[], int off, int len) throws IOException {
public void readFully(byte[] b, int off, int len) throws IOException {
try {
buf.get(b, off, len);
} catch (BufferUnderflowException x) {
Expand Down Expand Up @@ -307,6 +297,7 @@ public String readLine() throws IOException {
return new String(chars);
}

@NotNull
@Override
public String readUTF() throws IOException {
int length = 0;
Expand Down Expand Up @@ -341,48 +332,4 @@ public String readUTF() throws IOException {

return new String(chars, 0, length);
}

public String readUTF(@NotNull final CharSequenceAdapterBuilder output,
@NotNull final StringCache<String> cache) throws IOException {
readUTF(output);
return cache.getCachedString(output);
}

public void readUTF(@NotNull final CharSequenceAdapterBuilder output) throws IOException {
int total = readUnsignedShort();

output.clear().reserveCapacity(total);

while (total > 0) {
final int b1 = buf.get();
if ((b1 & 0x80) == 0) {
output.append((char) (b1 & 0xff));
total--;
} else if ((b1 & 0xe0) == 0xc0) {
final int b2 = buf.get();
if ((b2 & 0xc0) != 0x80) {
throw new UTFDataFormatException("malformed second byte " + b2);
}
output.append((char) (((b1 & 0x1F) << 6) | (b2 & 0x3F)));
total -= 2;
} else if ((b1 & 0xf0) == 0xe0) {
final int b2 = buf.get();
final int b3 = buf.get();
if ((b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80) {
throw new UTFDataFormatException("malformed second byte " + b2 + " or third byte " + b3);
}
output.append((char) (((b1 & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F)));
total -= 3;
} else {
throw new UTFDataFormatException("malformed first byte " + b1);
}
}
}

public void read(ByteBuffer dest, int length) {
final int sourceLimit = buf.limit();
buf.limit(buf.position() + length); // Constrain buf.remaining() to length
dest.put(buf);
buf.limit(sourceLimit);
}
}

0 comments on commit b13af92

Please sign in to comment.