3232import java .io .IOException ;
3333import java .io .InputStream ;
3434import java .nio .BufferUnderflowException ;
35+ import java .nio .channels .Channels ;
36+ import java .nio .channels .ReadableByteChannel ;
3537
3638/**
3739 * {@link SockInputStream} is a inputstream based on a socket. Due to memcached
@@ -46,6 +48,8 @@ public final class SockInputStream extends InputStream {
4648 private SchoonerSockIO sock ;
4749 private int limit ;
4850 private int count = 0 ;
51+ private byte b ;
52+ private ByteArrayOutputStream bos = new ByteArrayOutputStream ();
4953
5054 /**
5155 * get length of to-be-read.
@@ -102,7 +106,7 @@ public final int read() throws IOException {
102106 if (count >= limit ) {
103107 return -1 ;
104108 }
105- byte b = 0 ;
109+ b = 0 ;
106110 try {
107111 b = sock .readBuf .get ();
108112 } catch (BufferUnderflowException e ) {
@@ -124,7 +128,9 @@ public final int read() throws IOException {
124128 */
125129 private final void readFromChannel () throws IOException {
126130 sock .readBuf .clear ();
127- sock .getChannel ().read (sock .readBuf );
131+ // sock.getChannel().read(sock.readBuf);
132+ ReadableByteChannel wrappedChannel = Channels .newChannel (sock .getChannel ().socket ().getInputStream ());
133+ wrappedChannel .read (sock .readBuf );
128134 sock .readBuf .flip ();
129135 }
130136
@@ -171,7 +177,7 @@ public final byte[] getBuffer() throws IOException {
171177 * @since 2.5.1
172178 */
173179 public final String getLine () throws IOException {
174- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
180+ bos . reset ();
175181 int b ;
176182 while ((b = read ()) != -1 ) {
177183 bos .write (b );
0 commit comments