Skip to content

Commit 61e4c1a

Browse files
committed
Make ParallelOutputStream compatible with Java 8
Which doesn't support `ByteBuffer#flip`.
1 parent 9ac5192 commit 61e4c1a

File tree

1 file changed

+12
-6
lines changed
  • platforms/core-configuration/configuration-cache/src/main/kotlin/org/gradle/internal/cc/impl/io

1 file changed

+12
-6
lines changed

platforms/core-configuration/configuration-cache/src/main/kotlin/org/gradle/internal/cc/impl/io/ParallelOutputStream.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.gradle.internal.cc.impl.io
1919
import org.gradle.internal.cc.base.debug
2020
import org.gradle.internal.cc.base.logger
2121
import java.io.OutputStream
22+
import java.nio.Buffer
2223
import java.util.Queue
2324
import java.util.concurrent.ArrayBlockingQueue
2425
import java.util.concurrent.ConcurrentLinkedQueue
@@ -58,7 +59,6 @@ object ParallelOutputStream {
5859
val readyQ = ConcurrentLinkedQueue<Packet>()
5960
val writer = thread(name = "CC writer", isDaemon = true, priority = Thread.NORM_PRIORITY) {
6061
try {
61-
val buffer = ByteArray(PacketPool.packetSize)
6262
createOutputStream().use { outputStream ->
6363
while (true) {
6464
val packet = readyQ.poll()
@@ -67,20 +67,18 @@ object ParallelOutputStream {
6767
Thread.yield()
6868
continue
6969
}
70-
packet.flip()
70+
flip(packet)
7171
if (!packet.hasRemaining()) {
7272
/** producer is signaling end of stream
7373
* see [QueuedOutputStream.close]
7474
**/
7575
break
7676
}
7777
try {
78-
val remaining = packet.remaining()
79-
packet.get(buffer, 0, remaining)
80-
outputStream.write(buffer, 0, remaining)
78+
outputStream.write(packet.array(), 0, packet.remaining())
8179
} finally {
8280
// always return the packet to the pool
83-
packet.flip()
81+
flip(packet)
8482
packets.put(packet)
8583
}
8684
}
@@ -100,6 +98,14 @@ object ParallelOutputStream {
10098
writer.join()
10199
}
102100
}
101+
102+
private
103+
fun flip(packet: Buffer) {
104+
// packet.flip() is not available in Java 8
105+
packet
106+
.limit(packet.position())
107+
.rewind()
108+
}
103109
}
104110

105111

0 commit comments

Comments
 (0)