Skip to content

Commit

Permalink
UTF-8 naming cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pull-vert committed Oct 13, 2024
1 parent 6af31c4 commit a5a9044
Show file tree
Hide file tree
Showing 53 changed files with 961 additions and 1,017 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ try (var serverSocket = new ServerSocket(freePortNumber)) {
var serverThread = Thread.startVirtualThread(() -> {
try (var acceptedSocketEndpoint = SocketEndpoint.from(serverSocket.accept());
var serverWriter = Jayo.buffer(acceptedSocketEndpoint.getWriter())) {
serverWriter.writeUtf8("The Answer to the Ultimate Question of Life is ")
serverWriter.write("The Answer to the Ultimate Question of Life is ")
.writeUtf8CodePoint('4')
.writeUtf8CodePoint('2');
} catch (IOException e) {
Expand All @@ -48,7 +48,7 @@ try (var serverSocket = new ServerSocket(freePortNumber)) {
});
try (var clientSocketEndpoint = SocketEndpoint.from(new Socket("localhost", freePortNumber));
var clientReader = Jayo.buffer(clientSocketEndpoint.getReader())) {
assertThat(clientReader.readUtf8String())
assertThat(clientReader.readString())
.isEqualTo("The Answer to the Ultimate Question of Life is 42");
}
serverThread.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ open class BufferLatin1Benchmark {
// Prepare a string and ByteString for encoding and decoding with Okio and Jayo
jayoBuffer = Buffer()
val tempJayoIo = Buffer()
tempJayoIo.writeString(encode, Charsets.ISO_8859_1)
tempJayoIo.write(encode, Charsets.ISO_8859_1)
jayoDecode = tempJayoIo.snapshot()

okioBuffer = okio.Buffer()
Expand All @@ -84,13 +84,13 @@ open class BufferLatin1Benchmark {

@Benchmark
fun writeLatin1Jayo() {
jayoBuffer.writeString(encode, Charsets.ISO_8859_1)
jayoBuffer.write(encode, Charsets.ISO_8859_1)
jayoBuffer.clear()
}

@Benchmark
fun readLatin1Jayo(): String {
jayoBuffer.writeString(encode, Charsets.ISO_8859_1)
jayoBuffer.write(encode, Charsets.ISO_8859_1)
return jayoBuffer.readString(Charsets.ISO_8859_1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ open class BufferUtf8Benchmark {
// Prepare a string and ByteString for encoding and decoding with Okio and Jayo
jayoBuffer = Buffer()
val tempJayoIo = Buffer()
tempJayoIo.writeUtf8(encode)
tempJayoIo.write(encode)
jayoDecode = tempJayoIo.snapshot()

okioBuffer = okio.Buffer()
Expand All @@ -100,7 +100,7 @@ open class BufferUtf8Benchmark {

@Benchmark
fun writeUtf8Jayo() {
jayoBuffer.writeUtf8(encode)
jayoBuffer.write(encode)
jayoBuffer.clear()
}

Expand All @@ -113,7 +113,7 @@ open class BufferUtf8Benchmark {
@Benchmark
fun readUtf8StringJayo(): Int {
jayoBuffer.write(jayoDecode)
return jayoBuffer.readUtf8String().length
return jayoBuffer.readString().length
}

@Benchmark
Expand Down
64 changes: 27 additions & 37 deletions core/src/main/java/jayo/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,79 +252,69 @@ Buffer copyTo(final @NonNull Buffer out,

@Override
@NonNull
Buffer write(final @NonNull ByteString byteString);
Buffer write(final byte @NonNull [] source);

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
@NonNull
Buffer write(final @NonNull ByteString byteString,
final @NonNegative int offset, final @NonNegative int byteCount);
Buffer write(final byte @NonNull [] source,
final @NonNegative int offset,
final @NonNegative int byteCount);

@Override
@NonNull
Buffer writeUtf8(final @NonNull Utf8 utf8);
Buffer write(final @NonNull ByteString byteString);

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
@NonNull
Buffer writeUtf8(final @NonNull Utf8 utf8,
final @NonNegative int offset, final @NonNegative int byteCount);
Buffer write(final @NonNull ByteString byteString,
final @NonNegative int offset,
final @NonNegative int byteCount);

@Override
@NonNull
Buffer write(final byte @NonNull [] source);
Buffer write(final @NonNull CharSequence charSequence);

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
@NonNull
Buffer write(final byte @NonNull [] source,
final @NonNegative int offset, final @NonNegative int byteCount);
Buffer write(final @NonNull CharSequence charSequence,
final @NonNegative int startIndex,
final @NonNegative int endIndex);

/**
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
@NonNull
Buffer write(final @NonNull RawReader reader, final @NonNegative long byteCount);
Buffer writeUtf8CodePoint(final @NonNegative int codePoint);

@Override
@NonNull
Buffer writeUtf8(final @NonNull CharSequence charSequence);
Buffer write(final @NonNull String string, final @NonNull Charset charset);

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
@NonNull
Buffer writeUtf8(final @NonNull CharSequence charSequence,
final @NonNegative int startIndex,
final @NonNegative int endIndex);

@Override
@NonNull
Buffer writeUtf8CodePoint(final @NonNegative int codePoint);

@Override
@NonNull
Buffer writeString(final @NonNull String string, final @NonNull Charset charset);
Buffer write(final @NonNull String string,
final @NonNegative int startIndex,
final @NonNegative int endIndex,
final @NonNull Charset charset);

/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
@NonNull
Buffer writeString(final @NonNull String string,
@NonNegative int startIndex,
@NonNegative int endIndex,
@NonNull Charset charset);
Buffer write(final @NonNull RawReader reader, final @NonNegative long byteCount);

@Override
@NonNull
Expand Down Expand Up @@ -489,7 +479,7 @@ Buffer writeString(final @NonNull String string,
* its new data there. This single segment has a byte array of 16_709 bytes but only 7 bytes of data in it:
* <pre>
* {@code
* buffer.writeUtf8("unicorn");
* buffer.write("unicorn");
*
* // [ 'u', 'n', 'i', 'c', 'o', 'r', 'n', '?', '?', '?', ...]
* // ^ ^
Expand All @@ -516,7 +506,7 @@ Buffer writeString(final @NonNull String string,
* <pre>
* {@code
* val xoxo = Buffer()
* xoxo.writeUtf8("xo".repeat(10_000))
* xoxo.write("xo".repeat(10_000))
*
* // [ 'x', 'o', 'x', 'o', 'x', 'o', 'x', 'o', ..., 'x', 'o', 'x']
* // ^ ^
Expand All @@ -538,7 +528,7 @@ Buffer writeString(final @NonNull String string,
* <pre>
* {@code
* Buffer abc = Buffer.create();
* abc.writeUtf8("abc");
* abc.write("abc");
*
* // [ 'a', 'b', 'c', 'o', 'x', 'o', 'x', 'o', ...]
* // ^ ^
Expand All @@ -551,15 +541,15 @@ Buffer writeString(final @NonNull String string,
* <pre>
* {@code
* val nana = Buffer()
* nana.writeUtf8("na".repeat(2_500))
* nana.write("na".repeat(2_500))
* nana.readUtf8(2) // reads "na"
*
* // [ 'n', 'a', 'n', 'a', ..., 'n', 'a', 'n', 'a', '?', '?', '?', ...]
* // ^ ^
* // pos = 2 limit = 5000
*
* nana2 = nana.clone()
* nana2.writeUtf8("batman")
* nana2.write("batman")
*
* // this segment and its byte[] is shared between nana and nana2 buffers
* // ↓
Expand All @@ -576,7 +566,7 @@ Buffer writeString(final @NonNull String string,
* fragmentation in sharing-heavy use cases.
*
* <h2>Unsafe Cursor API</h2>
*
* <p>
* This class exposes privileged access to the internal byte arrays of a buffer. A cursor either references the
* data of a single segment, it is before the first segment ({@code offset == -1}), or it is after the last segment
* ({@code offset == buffer.byteSize()}).
Expand Down
Loading

0 comments on commit a5a9044

Please sign in to comment.