Skip to content
Closed
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
4 changes: 2 additions & 2 deletions okio-nodefilesystem/src/commonMain/kotlin/okio/FileSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal class FileSink(
) : Sink {
private var closed = false

override fun write(source: Buffer, byteCount: Long) {
override fun write(source: BufferedSource, byteCount: Long) {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
require(source.size >= byteCount) { "source.size=${source.size} < byteCount=$byteCount" }
require(source.buffer.size >= byteCount) { "source.size=${source.buffer.size} < byteCount=$byteCount" }
check(!closed) { "closed" }

val data = source.readByteArray(byteCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class FileSource(
private var position_ = 0L
private var closed = false

override fun read(sink: Buffer, byteCount: Long): Long {
override fun read(sink: BufferedSink, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
check(!closed) { "closed" }

Expand Down
6 changes: 3 additions & 3 deletions okio-wasifilesystem/src/wasmWasiMain/kotlin/okio/FileSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal class FileSink(
private var closed = false
private val cursor = Buffer.UnsafeCursor()

override fun write(source: Buffer, byteCount: Long) {
override fun write(source: BufferedSource, byteCount: Long) {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
require(source.size >= byteCount) { "source.size=${source.size} < byteCount=$byteCount" }
require(source.buffer.size >= byteCount) { "source.size=${source.buffer.size} < byteCount=$byteCount" }
check(!closed) { "closed" }

var bytesRemaining = byteCount
source.readAndWriteUnsafe(cursor)
source.buffer.readAndWriteUnsafe(cursor)
try {
while (bytesRemaining > 0L) {
check(cursor.next() != -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ internal class FileSource(
private val unsafeCursor = Buffer.UnsafeCursor()
private var closed = false

override fun read(sink: Buffer, byteCount: Long): Long {
override fun read(sink: BufferedSink, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
check(!closed) { "closed" }
val sinkInitialSize = sink.size
val sinkInitialSize = sink.buffer.size

// Request a writable segment in `sink`. We request at least 1024 bytes, unless the request is
// for smaller than that, in which case we request only that many bytes.
val cursor = sink.readAndWriteUnsafe(unsafeCursor)
val cursor = sink.buffer.readAndWriteUnsafe(unsafeCursor)
val addedCapacityCount = cursor.expandBuffer(minByteCount = minOf(byteCount, 1024L).toInt())

// Now that we have a writable segment, figure out how many bytes to read. This is the smaller
Expand Down
6 changes: 3 additions & 3 deletions okio/src/commonMain/kotlin/okio/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ expect class Buffer() : BufferedSource, BufferedSink {

/** Copy `byteCount` bytes from this, starting at `offset`, to `out`. */
fun copyTo(
out: Buffer,
out: BufferedSink,
offset: Long = 0L,
byteCount: Long,
): Buffer
Expand Down Expand Up @@ -124,7 +124,7 @@ expect class Buffer() : BufferedSource, BufferedSink {
override fun peek(): BufferedSource
override fun rangeEquals(offset: Long, bytes: ByteString): Boolean
override fun rangeEquals(offset: Long, bytes: ByteString, bytesOffset: Int, byteCount: Int): Boolean
override fun read(sink: Buffer, byteCount: Long): Long
override fun read(sink: BufferedSink, byteCount: Long): Long
override fun read(sink: ByteArray): Int
override fun read(sink: ByteArray, offset: Int, byteCount: Int): Int
override fun readAll(sink: Sink): Long
Expand Down Expand Up @@ -156,7 +156,7 @@ expect class Buffer() : BufferedSource, BufferedSink {
override fun timeout(): Timeout
override fun write(byteString: ByteString): Buffer
override fun write(byteString: ByteString, offset: Int, byteCount: Int): Buffer
override fun write(source: Buffer, byteCount: Long)
override fun write(source: BufferedSource, byteCount: Long)
override fun write(source: ByteArray): Buffer
override fun write(source: ByteArray, offset: Int, byteCount: Int): Buffer
override fun write(source: Source, byteCount: Long): Buffer
Expand Down
22 changes: 11 additions & 11 deletions okio/src/commonMain/kotlin/okio/FileHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ abstract class FileHandle(
@Throws(IOException::class)
protected abstract fun protectedClose()

private fun readNoCloseCheck(fileOffset: Long, sink: Buffer, byteCount: Long): Long {
private fun readNoCloseCheck(fileOffset: Long, sink: BufferedSink, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }

var currentOffset = fileOffset
val targetOffset = fileOffset + byteCount

while (currentOffset < targetOffset) {
val tail = sink.writableSegment(1)
val tail = sink.buffer.writableSegment(1)
val readByteCount = protectedRead(
fileOffset = currentOffset,
array = tail.data,
Expand All @@ -348,7 +348,7 @@ abstract class FileHandle(
if (readByteCount == -1) {
if (tail.pos == tail.limit) {
// We allocated a tail segment, but didn't end up needing it. Recycle!
sink.head = tail.pop()
sink.buffer.head = tail.pop()
SegmentPool.recycle(tail)
}
if (fileOffset == currentOffset) return -1L // We wanted bytes but didn't return any.
Expand All @@ -357,29 +357,29 @@ abstract class FileHandle(

tail.limit += readByteCount
currentOffset += readByteCount
sink.size += readByteCount
sink.buffer.size += readByteCount
}

return currentOffset - fileOffset
}

private fun writeNoCloseCheck(fileOffset: Long, source: Buffer, byteCount: Long) {
checkOffsetAndCount(source.size, 0L, byteCount)
private fun writeNoCloseCheck(fileOffset: Long, source: BufferedSource, byteCount: Long) {
checkOffsetAndCount(source.buffer.size, 0L, byteCount)

var currentOffset = fileOffset
val targetOffset = fileOffset + byteCount

while (currentOffset < targetOffset) {
val head = source.head!!
val head = source.buffer.head!!
val toCopy = minOf(targetOffset - currentOffset, head.limit - head.pos).toInt()
protectedWrite(currentOffset, head.data, head.pos, toCopy)

head.pos += toCopy
currentOffset += toCopy
source.size -= toCopy
source.buffer.size -= toCopy

if (head.pos == head.limit) {
source.head = head.pop()
source.buffer.head = head.pop()
SegmentPool.recycle(head)
}
}
Expand All @@ -391,7 +391,7 @@ abstract class FileHandle(
) : Sink {
var closed = false

override fun write(source: Buffer, byteCount: Long) {
override fun write(source: BufferedSource, byteCount: Long) {
check(!closed) { "closed" }
fileHandle.writeNoCloseCheck(position, source, byteCount)
position += byteCount
Expand Down Expand Up @@ -421,7 +421,7 @@ abstract class FileHandle(
) : Source {
var closed = false

override fun read(sink: Buffer, byteCount: Long): Long {
override fun read(sink: BufferedSink, byteCount: Long): Long {
check(!closed) { "closed" }
val result = fileHandle.readNoCloseCheck(position, sink, byteCount)
if (result != -1L) position += result
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/ForwardingSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ expect abstract class ForwardingSource constructor(
// TODO 'Source by delegate' once https://youtrack.jetbrains.com/issue/KT-23935 is fixed.

@Throws(IOException::class)
override fun read(sink: Buffer, byteCount: Long): Long
override fun read(sink: BufferedSink, byteCount: Long): Long

override fun timeout(): Timeout

Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/HashingSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ expect class HashingSink : Sink {
override fun close()
override fun flush()
override fun timeout(): Timeout
override fun write(source: Buffer, byteCount: Long)
override fun write(source: BufferedSource, byteCount: Long)

companion object {
/**
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/HashingSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ expect class HashingSource : Source {
val hash: ByteString

override fun close()
override fun read(sink: Buffer, byteCount: Long): Long
override fun read(sink: BufferedSink, byteCount: Long): Long
override fun timeout(): Timeout

companion object {
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/Okio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun Sink.buffer(): BufferedSink = RealBufferedSink(this)
fun blackholeSink(): Sink = BlackholeSink()

private class BlackholeSink : Sink {
override fun write(source: Buffer, byteCount: Long) = source.skip(byteCount)
override fun write(source: BufferedSource, byteCount: Long) = source.skip(byteCount)
override fun flush() {}
override fun timeout() = Timeout.NONE
override fun close() {}
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/PeekSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class PeekSource(
private var closed = false
private var pos = 0L

override fun read(sink: Buffer, byteCount: Long): Long {
override fun read(sink: BufferedSink, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
check(!closed) { "closed" }
// Source becomes invalid if there is an expected Segment and it and the expected position
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/RealBufferedSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal expect class RealBufferedSink(
override fun timeout(): Timeout
override fun write(byteString: ByteString): BufferedSink
override fun write(byteString: ByteString, offset: Int, byteCount: Int): BufferedSink
override fun write(source: Buffer, byteCount: Long)
override fun write(source: BufferedSource, byteCount: Long)
override fun write(source: ByteArray): BufferedSink
override fun write(source: ByteArray, offset: Int, byteCount: Int): BufferedSink
override fun write(source: Source, byteCount: Long): BufferedSink
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/RealBufferedSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal expect class RealBufferedSource(
override fun peek(): BufferedSource
override fun rangeEquals(offset: Long, bytes: ByteString): Boolean
override fun rangeEquals(offset: Long, bytes: ByteString, bytesOffset: Int, byteCount: Int): Boolean
override fun read(sink: Buffer, byteCount: Long): Long
override fun read(sink: BufferedSink, byteCount: Long): Long
override fun read(sink: ByteArray): Int
override fun read(sink: ByteArray, offset: Int, byteCount: Int): Int
override fun readAll(sink: Sink): Long
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/Sink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package okio
expect interface Sink : Closeable {
/** Removes `byteCount` bytes from `source` and appends them to this. */
@Throws(IOException::class)
fun write(source: Buffer, byteCount: Long)
fun write(source: BufferedSource, byteCount: Long)

/** Pushes all buffered bytes to their final destination. */
@Throws(IOException::class)
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface Source : Closeable {
* the number of bytes read, or -1 if this source is exhausted.
*/
@Throws(IOException::class)
fun read(sink: Buffer, byteCount: Long): Long
fun read(sink: BufferedSink, byteCount: Long): Long

/** Returns the timeout for this source. */
fun timeout(): Timeout
Expand Down
32 changes: 17 additions & 15 deletions okio/src/commonMain/kotlin/okio/internal/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import kotlin.jvm.JvmName
import okio.ArrayIndexOutOfBoundsException
import okio.Buffer
import okio.Buffer.UnsafeCursor
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import okio.EOFException
import okio.Options
Expand Down Expand Up @@ -234,7 +236,7 @@ internal fun Buffer.selectPrefix(options: Options, selectTruncated: Boolean = fa
// have to call these functions. Remove all this nonsense when expect class allow actual code.

internal inline fun Buffer.commonCopyTo(
out: Buffer,
out: BufferedSink,
offset: Long,
byteCount: Long,
): Buffer {
Expand All @@ -243,7 +245,7 @@ internal inline fun Buffer.commonCopyTo(
checkOffsetAndCount(size, offset, byteCount)
if (byteCount == 0L) return this

out.size += byteCount
out.buffer.size += byteCount

// Skip segments that we aren't copying from.
var s = head
Expand All @@ -257,12 +259,12 @@ internal inline fun Buffer.commonCopyTo(
val copy = s!!.sharedCopy()
copy.pos += offset.toInt()
copy.limit = minOf(copy.pos + byteCount.toInt(), copy.limit)
if (out.head == null) {
if (out.buffer.head == null) {
copy.prev = copy
copy.next = copy.prev
out.head = copy.next
out.buffer.head = copy.next
} else {
out.head!!.prev!!.push(copy)
out.buffer.head!!.prev!!.push(copy)
}
byteCount -= (copy.limit - copy.pos).toLong()
offset = 0L
Expand Down Expand Up @@ -1148,7 +1150,7 @@ internal inline fun Buffer.commonWriteLong(v: Long): Buffer {
return this
}

internal inline fun Buffer.commonWrite(source: Buffer, byteCount: Long) {
internal inline fun Buffer.commonWrite(source: BufferedSource, byteCount: Long) {
var byteCount = byteCount
// Move bytes from the head of the source buffer to the tail of this buffer
// while balancing two conflicting goals: don't waste CPU and don't waste
Expand Down Expand Up @@ -1201,31 +1203,31 @@ internal inline fun Buffer.commonWrite(source: Buffer, byteCount: Long) {
// yielding sink [51%, 91%, 30%] and source [62%, 82%].

require(source !== this) { "source == this" }
checkOffsetAndCount(source.size, 0, byteCount)
checkOffsetAndCount(source.buffer.size, 0, byteCount)

while (byteCount > 0L) {
// Is a prefix of the source's head segment all that we need to move?
if (byteCount < source.head!!.limit - source.head!!.pos) {
if (byteCount < source.buffer.head!!.limit - source.buffer.head!!.pos) {
val tail = if (head != null) head!!.prev else null
if (tail != null && tail.owner &&
byteCount + tail.limit - (if (tail.shared) 0 else tail.pos) <= Segment.SIZE
) {
// Our existing segments are sufficient. Move bytes from source's head to our tail.
source.head!!.writeTo(tail, byteCount.toInt())
source.size -= byteCount
source.buffer.head!!.writeTo(tail, byteCount.toInt())
source.buffer.size -= byteCount
size += byteCount
return
} else {
// We're going to need another segment. Split the source's head
// segment in two, then move the first of those two to this buffer.
source.head = source.head!!.split(byteCount.toInt())
source.buffer.head = source.buffer.head!!.split(byteCount.toInt())
}
}

// Remove the source's head segment and append it to our tail.
val segmentToMove = source.head
val segmentToMove = source.buffer.head
val movedByteCount = (segmentToMove!!.limit - segmentToMove.pos).toLong()
source.head = segmentToMove.pop()
source.buffer.head = segmentToMove.pop()
if (head == null) {
head = segmentToMove
segmentToMove.prev = segmentToMove
Expand All @@ -1235,13 +1237,13 @@ internal inline fun Buffer.commonWrite(source: Buffer, byteCount: Long) {
tail = tail!!.push(segmentToMove)
tail.compact()
}
source.size -= movedByteCount
source.buffer.size -= movedByteCount
size += movedByteCount
byteCount -= movedByteCount
}
}

internal inline fun Buffer.commonRead(sink: Buffer, byteCount: Long): Long {
internal inline fun Buffer.commonRead(sink: BufferedSink, byteCount: Long): Long {
var byteCount = byteCount
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
if (size == 0L) return -1L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package okio.internal

import kotlin.jvm.JvmName
import okio.Buffer
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import okio.EOFException
Expand All @@ -35,7 +36,7 @@ import okio.buffer
import okio.checkOffsetAndCount
import okio.minOf

internal inline fun RealBufferedSource.commonRead(sink: Buffer, byteCount: Long): Long {
internal inline fun RealBufferedSource.commonRead(sink: BufferedSink, byteCount: Long): Long {
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
check(!closed) { "closed" }

Expand Down
4 changes: 2 additions & 2 deletions okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum class BufferedSourceFactory {
return Pipe(
buffer,
object : Source by buffer {
override fun read(sink: Buffer, byteCount: Long): Long {
override fun read(sink: BufferedSink, byteCount: Long): Long {
// Read one byte into a new buffer, then clone it so that the segment is shared.
// Shared segments cannot be compacted so we'll get a long chain of short segments.
val box = Buffer()
Expand All @@ -77,7 +77,7 @@ enum class BufferedSourceFactory {
val buffer = Buffer()
return Pipe(
object : Sink by buffer {
override fun write(source: Buffer, byteCount: Long) {
override fun write(source: BufferedSource, byteCount: Long) {
// Write each byte into a new buffer, then clone it so that the segments are shared.
// Shared segments cannot be compacted so we'll get a long chain of short segments.
for (i in 0 until byteCount) {
Expand Down
Loading
Loading