File tree Expand file tree Collapse file tree 2 files changed +21
-8
lines changed Expand file tree Collapse file tree 2 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -488,7 +488,7 @@ public class Buffer : Source, Sink {
488
488
489
489
while (remainingByteCount > 0L ) {
490
490
// Is a prefix of the source's head segment all that we need to move?
491
- if (remainingByteCount < source.head!! .limit - source.head !! .pos ) {
491
+ if (remainingByteCount < source.head!! .size ) {
492
492
val tail = tail
493
493
if (tail != null && tail.owner &&
494
494
remainingByteCount + tail.limit - (if (tail.shared) 0 else tail.pos) <= Segment .SIZE
@@ -501,17 +501,13 @@ public class Buffer : Source, Sink {
501
501
} else {
502
502
// We're going to need another segment. Split the source's head
503
503
// segment in two, then move the first of those two to this buffer.
504
- val newHead = source.head!! .split(remainingByteCount.toInt())
505
- if (source.head == source.tail) {
506
- source.tail = newHead
507
- }
508
- source.head = newHead
504
+ source.head = source.head!! .split(remainingByteCount.toInt())
509
505
}
510
506
}
511
507
512
508
// Remove the source's head segment and append it to our tail.
513
- val segmentToMove = source.head
514
- val movedByteCount = ( segmentToMove!! .limit - segmentToMove.pos) .toLong()
509
+ val segmentToMove = source.head!!
510
+ val movedByteCount = segmentToMove.size .toLong()
515
511
source.head = segmentToMove.pop()
516
512
if (source.head == null ) {
517
513
source.tail = null
Original file line number Diff line number Diff line change @@ -601,4 +601,21 @@ class CommonBufferTest {
601
601
buffer.clear()
602
602
assertEquals(ByteString (), buffer.snapshot())
603
603
}
604
+
605
+ @Test
606
+ fun splitHead () {
607
+ val dst = Buffer ()
608
+ val src = Buffer ().also { it.write(ByteArray (SEGMENT_SIZE )) }
609
+
610
+ dst.write(src, src.size / 2 )
611
+ assertEquals(src.size, dst.size)
612
+
613
+ assertEquals(src.head, src.tail)
614
+ assertEquals(null , src.head?.prev)
615
+ assertEquals(null , src.tail?.next)
616
+
617
+ assertEquals(dst.head, dst.tail)
618
+ assertEquals(null , dst.head?.prev)
619
+ assertEquals(null , dst.tail?.next)
620
+ }
604
621
}
You can’t perform that action at this time.
0 commit comments