Permalink
Browse files

Fixed #957

  • Loading branch information...
mpilquist committed Jan 8, 2018
1 parent 6acb9bd commit 583b01475ea7836aeead86270eb4e7b2f3668ee8
Showing with 12 additions and 6 deletions.
  1. +1 −0 .gitignore
  2. +11 −6 io/src/main/scala/fs2/io/tcp/Socket.scala
View
@@ -14,3 +14,4 @@ z_local.sbt
.ensime
/.ensime_cache
.tags
.metaserver
@@ -202,25 +202,30 @@ protected[tcp] object Socket {
// buffer is also reset to be ready to be written into.
def getBufferOf(sz: Int): F[ByteBuffer] = {
bufferRef.get.flatMap { buff =>
if (buff.capacity() < sz) bufferRef.modify { _ => ByteBuffer.allocate(sz) } map { _.now }
else {
if (buff.capacity() < sz) {
bufferRef.modify { _ => ByteBuffer.allocate(sz) }.map { _.now }
} else {
buff.clear()
buff.limit(sz)
F.pure(buff)
}
}
}
// When the read operation is done, this will read up to buffer's position bytes from the buffer
// this expects the buffer's position to be at bytes read + 1
def releaseBuffer(buff: ByteBuffer): F[Chunk[Byte]] = {
def releaseBuffer(buff: ByteBuffer): F[Chunk[Byte]] = F.delay {
val read = buff.position()
if (read == 0) F.pure(Chunk.bytes(Array.empty))
else {
val result = if (read == 0) {
Chunk.bytes(Array.empty)
} else {
val dest = new Array[Byte](read)
buff.flip()
buff.get(dest)
F.pure(Chunk.bytes(dest))
Chunk.bytes(dest)
}
buff.clear()
result
}
def read0(max:Int, timeout:Option[FiniteDuration]):F[Option[Chunk[Byte]]] = {

0 comments on commit 583b014

Please sign in to comment.