Fix strings

This commit is contained in:
Andrea Cavalli 2023-04-20 02:10:53 +02:00
parent c22d6719d9
commit f9e5657a83
5 changed files with 17 additions and 97 deletions

View File

@ -2,8 +2,6 @@ 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;
@ -42,22 +40,4 @@ public class BufDataInput extends SafeDataInputStream {
public boolean markSupported() {
return false;
}
@Deprecated
@Override
public @NotNull String readUTF() {
return readShortText(StandardCharsets.UTF_8);
}
@Override
public @NotNull String readShortText(Charset charset) {
var length = this.readUnsignedShort();
return this.readString(length, charset);
}
@Override
public @NotNull String readMediumText(Charset charset) {
var length = this.readInt();
return this.readString(length, charset);
}
}

View File

@ -3,58 +3,21 @@ package it.cavallium.data.generator.nativedata;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.stream.SafeDataInput;
import it.cavallium.stream.SafeDataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;
public class StringSerializer implements DataSerializer<String> {
public static final StringSerializer INSTANCE = new StringSerializer();
private static final ThreadLocal<CharsetEncoder> UTF8_ENCODER = ThreadLocal.withInitial(() -> StandardCharsets.UTF_8
.newEncoder()
.onUnmappableCharacter(CodingErrorAction.REPORT)
.onMalformedInput(CodingErrorAction.REPORT)
);
private static final ThreadLocal<CharsetDecoder> UTF8_DECODER = ThreadLocal.withInitial(() -> StandardCharsets.UTF_8
.newDecoder()
.onUnmappableCharacter(CodingErrorAction.REPORT)
.onMalformedInput(CodingErrorAction.REPORT)
);
@Override
public void serialize(SafeDataOutput dataOutput, @NotNull String data) {
try {
var bytes = UTF8_ENCODER.get().reset().encode(CharBuffer.wrap(data));
dataOutput.writeInt(bytes.limit());
if (bytes.hasArray()) {
dataOutput.write(bytes.array(), bytes.arrayOffset(), bytes.limit());
} else {
while (bytes.hasRemaining()) {
dataOutput.writeByte(bytes.get());
}
}
} catch (CharacterCodingException ex) {
throw new IllegalStateException("Can't encode this UTF-8 string", ex);
}
dataOutput.writeMediumText(data, StandardCharsets.UTF_8);
}
@NotNull
@Override
public String deserialize(SafeDataInput dataInput) {
byte[] bytes = new byte[dataInput.readInt()];
dataInput.readFully(bytes);
try {
CharBuffer decoded = UTF8_DECODER.get().reset().decode(ByteBuffer.wrap(bytes));
return decoded.toString();
} catch (IllegalStateException | CharacterCodingException ex) {
throw new IllegalStateException("Can't decode this UTF-8 string", ex);
}
return dataInput.readMediumText(StandardCharsets.UTF_8);
}
}

View File

@ -4,6 +4,7 @@ import java.io.Closeable;
import java.io.DataInput;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;
/**
@ -145,9 +146,19 @@ public interface SafeDataInput extends Closeable, DataInput {
String readLine();
@Deprecated
@NotNull String readUTF();
default @NotNull String readUTF() {
return readShortText(StandardCharsets.UTF_8);
}
@NotNull String readShortText(Charset charset);
default @NotNull String readShortText(Charset charset) {
var length = this.readUnsignedShort();
return this.readString(length, charset);
}
@NotNull String readMediumText(Charset charset);
default @NotNull String readMediumText(Charset charset) {
var length = this.readInt();
return this.readString(length, charset);
}
@NotNull String readString(int len, Charset charset);
}

View File

@ -25,9 +25,6 @@
package it.cavallium.stream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import it.cavallium.buffer.IgnoreCoverage;
import org.jetbrains.annotations.NotNull;
@ -342,35 +339,4 @@ public class SafeDataInputStream extends SafeFilterInputStream implements SafeDa
public final String readLine() {
throw new UnsupportedOperationException();
}
/**
* 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.
*/
@Deprecated
@IgnoreCoverage
@Override
public @NotNull String readUTF() {
return readShortText(StandardCharsets.UTF_8);
}
@IgnoreCoverage
@Override
public @NotNull String readShortText(Charset charset) {
int utfLength = this.readUnsignedShort();
return in.readString(utfLength, charset);
}
@IgnoreCoverage
@Override
public @NotNull String readMediumText(Charset charset) {
int utfLength = this.readInt();
return in.readString(utfLength, charset);
}
}

View File

@ -224,7 +224,7 @@ public class SafeFilterInputStream extends SafeInputStream {
@IgnoreCoverage
@Override
public String readString(int length, Charset charset) {
public @NotNull String readString(int length, Charset charset) {
return in.readString(length, charset);
}
}