Fast string read

This commit is contained in:
Andrea Cavalli 2023-03-28 00:16:50 +02:00
parent 9faebd75a0
commit 7177f5944b
5 changed files with 44 additions and 1 deletions

View File

@ -2,6 +2,8 @@ package it.cavallium.buffer;
import it.cavallium.stream.SafeByteArrayInputStream;
import it.cavallium.stream.SafeDataInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;
@ -39,4 +41,16 @@ public class BufDataInput extends SafeDataInputStream {
public boolean markSupported() {
return false;
}
@Override
public @NotNull String readUTF() {
var length = this.readUnsignedShort();
this.skipNBytes(length);
return this.in.readString(length, StandardCharsets.UTF_8);
}
@Override
public String readString(int length, Charset charset) {
return in.readString(length, charset);
}
}

View File

@ -16,6 +16,8 @@
package it.cavallium.stream;
import java.nio.charset.Charset;
/** Simple, fast and repositionable byte-array input stream.
*
* <p><strong>Warning</strong>: this class implements the correct semantics
@ -119,6 +121,15 @@ public class SafeByteArrayInputStream extends SafeMeasurableInputStream implemen
return n;
}
@Override
public String readString(int length, Charset charset) {
if (this.available() < length) {
throw new IndexOutOfBoundsException(this.length);
}
position += length;
return new String(this.array, position, length, charset);
}
@Override
public long position() {
return position;

View File

@ -25,6 +25,7 @@
package it.cavallium.stream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;
@ -124,6 +125,11 @@ public class SafeDataInputStream extends SafeFilterInputStream implements SafeDa
return total;
}
@Override
public String readString(int length, Charset charset) {
return in.readString(length, charset);
}
/**
* See the general contract of the {@code readBoolean}
* method of {@code DataInput}.
@ -429,7 +435,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements SafeDa
* @see SafeDataInputStream#readUTF(SafeDataInputStream)
*/
@Override
public final @NotNull String readUTF() {
public @NotNull String readUTF() {
return readUTF(this);
}

View File

@ -1,5 +1,7 @@
package it.cavallium.stream;
import java.nio.charset.Charset;
/**
* A {@code FilterInputStream} contains
* some other input stream, which it uses as
@ -207,4 +209,9 @@ public class SafeFilterInputStream extends SafeInputStream {
public boolean markSupported() {
return in.markSupported();
}
@Override
public String readString(int length, Charset charset) {
return in.readString(length, charset);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.stream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -126,6 +127,10 @@ public abstract class SafeInputStream extends InputStream {
return n;
}
public String readString(int length, Charset charset) {
return new String(readNBytes(length), charset);
}
public long skip(long n) {
long remaining = n;
int nr;