Skip to content

Commit cb52392

Browse files
committed
Refactor/fix the FrameReader to read plain bytes instead of lines
1 parent 97f0e8d commit cb52392

File tree

2 files changed

+25
-8
lines changed
  • api-client/src/main

2 files changed

+25
-8
lines changed

api-client/src/main/java/de/gesellix/docker/remote/api/core/Frame.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public byte[] getPayload() {
2121
}
2222

2323
public String getPayloadAsString() {
24+
if (payload == null) {
25+
return null;
26+
}
2427
return new String(payload, StandardCharsets.UTF_8).trim();
2528
}
2629

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
package de.gesellix.docker.remote.api.core
22

33
import de.gesellix.docker.response.Reader
4+
import okio.Buffer
45
import okio.BufferedSource
56
import okio.Source
67
import okio.buffer
78

89
class FrameReader(source: Source, private val expectMultiplexedResponse: Boolean = false) : Reader<Frame> {
910

10-
private val buffer: BufferedSource = source.buffer()
11+
private val bufferedSource: BufferedSource = source.buffer()
12+
13+
private val buffer = Buffer()
1114

1215
override fun readNext(type: Class<Frame>?): Frame {
1316
return if (expectMultiplexedResponse) {
1417
// See https://docs.docker.com/engine/api/v1.41/#operation/ContainerAttach for the stream format documentation.
1518
// header := [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}
1619

17-
val streamType = Frame.StreamType.valueOf(buffer.readByte())
18-
buffer.skip(3)
19-
val frameSize = buffer.readInt()
20+
val streamType = Frame.StreamType.valueOf(bufferedSource.readByte())
21+
bufferedSource.skip(3)
22+
val frameSize = bufferedSource.readInt()
2023

21-
Frame(streamType, buffer.readByteArray(frameSize.toLong()))
24+
Frame(streamType, bufferedSource.readByteArray(frameSize.toLong()))
2225
} else {
23-
// TODO consider reading plain bytes, not line separated
24-
Frame(Frame.StreamType.RAW, buffer.readUtf8Line()?.encodeToByteArray())
26+
var byteCount: Long
27+
bufferedSource.read(buffer, 8192L).also { byteCount = it }
28+
if (byteCount < 0) {
29+
Frame(Frame.StreamType.RAW, null)
30+
} else {
31+
Frame(Frame.StreamType.RAW, buffer.readByteArray(byteCount))
32+
}
2533
}
2634
}
2735

2836
override fun hasNext(): Boolean {
29-
return !Thread.currentThread().isInterrupted && !buffer.exhausted()
37+
return try {
38+
!Thread.currentThread().isInterrupted
39+
// && bufferedSource.isOpen
40+
&& !bufferedSource.peek().exhausted()
41+
} catch (e: Exception) {
42+
return false
43+
}
3044
}
3145
}

0 commit comments

Comments
 (0)