Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BufferedSink.write(ByteString, offset, byteCount) #651

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions okio/src/commonMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ expect class Buffer() : BufferedSource, BufferedSink {

override fun write(byteString: ByteString): Buffer

override fun write(byteString: ByteString, offset: Int, byteCount: Int): Buffer

override fun writeUtf8(string: String): Buffer

override fun writeUtf8(string: String, beginIndex: Int, endIndex: Int): Buffer
Expand Down
2 changes: 2 additions & 0 deletions okio/src/commonMain/kotlin/okio/BufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ expect interface BufferedSink : Sink {

fun write(byteString: ByteString): BufferedSink

fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink

/** Like [OutputStream.write], this writes a complete byte array to this sink. */
fun write(source: ByteArray): BufferedSink

Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal constructor(data: ByteArray) : Comparable<ByteString> {
fun toByteArray(): ByteArray

/** Writes the contents of this byte string to `buffer`. */
internal fun write(buffer: Buffer)
internal fun write(buffer: Buffer, offset: Int, byteCount: Int)

/** Returns the bytes of this string without a defensive copy. Do not mutate! */
internal fun internalArray(): ByteArray
Expand Down
8 changes: 6 additions & 2 deletions okio/src/commonMain/kotlin/okio/internal/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,12 @@ internal inline fun Buffer.commonSkip(byteCount: Long) {
}
}

internal inline fun Buffer.commonWrite(byteString: ByteString): Buffer {
byteString.write(this)
internal inline fun Buffer.commonWrite(
byteString: ByteString,
offset: Int = 0,
byteCount: Int = byteString.size
): Buffer {
byteString.write(this, offset, byteCount)
return this
}

Expand Down
4 changes: 2 additions & 2 deletions okio/src/commonMain/kotlin/okio/internal/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ internal inline fun String.commonDecodeHex(): ByteString {
}

/** Writes the contents of this byte string to `buffer`. */
internal fun ByteString.commonWrite(buffer: Buffer) {
buffer.write(data, 0, data.size)
internal fun ByteString.commonWrite(buffer: Buffer, offset: Int, byteCount: Int) {
buffer.write(data, offset, byteCount)
}

private fun decodeHexDigit(c: Char): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ internal inline fun SegmentedByteString.commonToByteArray(): ByteArray {
return result
}

internal inline fun SegmentedByteString.commonWrite(buffer: Buffer) {
forEachSegment { data, offset, byteCount ->
internal inline fun SegmentedByteString.commonWrite(buffer: Buffer, offset: Int, byteCount: Int) {
forEachSegment(offset, offset + byteCount) { data, offset, byteCount ->
val segment = Segment(data, offset, offset + byteCount, true, false)
if (buffer.head == null) {
segment.prev = segment
Expand Down
3 changes: 3 additions & 0 deletions okio/src/jsMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ actual class Buffer : BufferedSource, BufferedSink {

actual override fun write(byteString: ByteString): Buffer = commonWrite(byteString)

actual override fun write(byteString: ByteString, offset: Int, byteCount: Int) =
commonWrite(byteString, offset, byteCount)

internal actual fun writableSegment(minimumCapacity: Int): Segment =
commonWritableSegment(minimumCapacity)

Expand Down
2 changes: 2 additions & 0 deletions okio/src/jsMain/kotlin/okio/BufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ actual interface BufferedSink : Sink {

actual fun write(byteString: ByteString): BufferedSink

actual fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink

actual fun write(source: ByteArray): BufferedSink

actual fun write(source: ByteArray, offset: Int, byteCount: Int): BufferedSink
Expand Down
3 changes: 2 additions & 1 deletion okio/src/jsMain/kotlin/okio/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ internal actual constructor(
/** Returns the bytes of this string without a defensive copy. Do not mutate! */
internal actual open fun internalArray() = commonInternalArray()

internal actual open fun write(buffer: Buffer) = commonWrite(buffer)
internal actual open fun write(buffer: Buffer, offset: Int, byteCount: Int) =
commonWrite(buffer, offset, byteCount)

/**
* Returns true if the bytes of this in `[offset..offset+byteCount)` equal the bytes of `other` in
Expand Down
3 changes: 2 additions & 1 deletion okio/src/jsMain/kotlin/okio/SegmentedByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ internal actual class SegmentedByteString internal actual constructor(

override fun toByteArray(): ByteArray = commonToByteArray()

override fun write(buffer: Buffer): Unit = commonWrite(buffer)
override fun write(buffer: Buffer, offset: Int, byteCount: Int): Unit =
commonWrite(buffer, offset, byteCount)

override fun rangeEquals(
offset: Int,
Expand Down
3 changes: 3 additions & 0 deletions okio/src/jvmMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ actual class Buffer : BufferedSource, BufferedSink, Cloneable, ByteChannel {

actual override fun write(byteString: ByteString): Buffer = commonWrite(byteString)

actual override fun write(byteString: ByteString, offset: Int, byteCount: Int) =
commonWrite(byteString, offset, byteCount)

actual override fun writeUtf8(string: String): Buffer = writeUtf8(string, 0, string.length)

actual override fun writeUtf8(string: String, beginIndex: Int, endIndex: Int): Buffer =
Expand Down
3 changes: 3 additions & 0 deletions okio/src/jvmMain/kotlin/okio/BufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ actual interface BufferedSink : Sink, WritableByteChannel {
@Throws(IOException::class)
actual fun write(byteString: ByteString): BufferedSink

@Throws(IOException::class)
actual fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink

@Throws(IOException::class)
actual fun write(source: ByteArray): BufferedSink

Expand Down
3 changes: 2 additions & 1 deletion okio/src/jvmMain/kotlin/okio/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ internal actual constructor(
out.write(data)
}

internal actual open fun write(buffer: Buffer) = commonWrite(buffer)
internal actual open fun write(buffer: Buffer, offset: Int, byteCount: Int) =
commonWrite(buffer, offset, byteCount)

actual open fun rangeEquals(
offset: Int,
Expand Down
6 changes: 6 additions & 0 deletions okio/src/jvmMain/kotlin/okio/RealBufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ internal class RealBufferedSink(
return emitCompleteSegments()
}

override fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink {
check(!closed) { "closed" }
buffer.write(byteString, offset, byteCount)
return emitCompleteSegments()
}

override fun writeUtf8(string: String): BufferedSink {
check(!closed) { "closed" }
buffer.writeUtf8(string)
Expand Down
3 changes: 2 additions & 1 deletion okio/src/jvmMain/kotlin/okio/SegmentedByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ internal actual class SegmentedByteString internal actual constructor(
}
}

override fun write(buffer: Buffer): Unit = commonWrite(buffer)
override fun write(buffer: Buffer, offset: Int, byteCount: Int): Unit =
commonWrite(buffer, offset, byteCount)

override fun rangeEquals(
offset: Int,
Expand Down
12 changes: 12 additions & 0 deletions okio/src/jvmTest/java/okio/BufferedSinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ public static List<Object[]> parameters() {
assertEquals("[hex=2143658701efcdab005cb1b0bebafeca]", data.toString());
}

@Test public void writeByteString() throws IOException {
sink.write(ByteString.encodeUtf8("təˈranəˌsôr"));
sink.flush();
assertEquals(ByteString.decodeHex("74c999cb8872616ec999cb8c73c3b472"), data.readByteString());
}

@Test public void writeByteStringOffset() throws IOException {
sink.write(ByteString.encodeUtf8("təˈranəˌsôr"), 5, 5);
sink.flush();
assertEquals(ByteString.decodeHex("72616ec999"), data.readByteString());
}

@Test public void writeStringUtf8() throws IOException {
sink.writeUtf8("təˈranəˌsôr");
sink.flush();
Expand Down
3 changes: 3 additions & 0 deletions okio/src/nativeMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ actual class Buffer : BufferedSource, BufferedSink {

actual override fun write(byteString: ByteString): Buffer = commonWrite(byteString)

actual override fun write(byteString: ByteString, offset: Int, byteCount: Int) =
commonWrite(byteString, offset, byteCount)

actual override fun writeUtf8(string: String): Buffer = writeUtf8(string, 0, string.length)

internal actual fun writableSegment(minimumCapacity: Int): Segment =
Expand Down
2 changes: 2 additions & 0 deletions okio/src/nativeMain/kotlin/okio/BufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ actual interface BufferedSink : Sink {

actual fun write(byteString: ByteString): BufferedSink

actual fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink

actual fun write(source: ByteArray): BufferedSink

actual fun write(source: ByteArray, offset: Int, byteCount: Int): BufferedSink
Expand Down
3 changes: 2 additions & 1 deletion okio/src/nativeMain/kotlin/okio/ByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ internal actual constructor(
/** Returns the bytes of this string without a defensive copy. Do not mutate! */
internal actual open fun internalArray() = commonInternalArray()

internal actual open fun write(buffer: Buffer) = commonWrite(buffer)
internal actual open fun write(buffer: Buffer, offset: Int, byteCount: Int) =
commonWrite(buffer, offset, byteCount)

/**
* Returns true if the bytes of this in `[offset..offset+byteCount)` equal the bytes of `other` in
Expand Down
3 changes: 2 additions & 1 deletion okio/src/nativeMain/kotlin/okio/SegmentedByteString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ internal actual class SegmentedByteString internal actual constructor(

override fun toByteArray(): ByteArray = commonToByteArray()

override fun write(buffer: Buffer): Unit = commonWrite(buffer)
override fun write(buffer: Buffer, offset: Int, byteCount: Int): Unit =
commonWrite(buffer, offset, byteCount)

override fun rangeEquals(
offset: Int,
Expand Down