|
| 1 | +/** |
| 2 | + * src/com/danga/MemCached/ByteBufArrayInputStream.java |
| 3 | + * |
| 4 | + * @author $Author: $ |
| 5 | + * @version $Revision: $ $Date: $ |
| 6 | + * copyright (c) 2006 meetup, inc |
| 7 | + * |
| 8 | + * $Id: $ |
| 9 | + */ |
| 10 | +package com.danga.MemCached; |
| 11 | + |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.nio.ByteBuffer; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public final class ByteBufArrayInputStream extends InputStream implements LineInputStream { |
| 20 | + private ByteBuffer[] bufs; |
| 21 | + private int currentBuf = 0; |
| 22 | + |
| 23 | + public ByteBufArrayInputStream( List<ByteBuffer> bufs ) throws Exception { |
| 24 | + this( bufs.toArray( new ByteBuffer[] {} ) ); |
| 25 | + } |
| 26 | + |
| 27 | + public ByteBufArrayInputStream( ByteBuffer[] bufs ) throws Exception { |
| 28 | + if ( bufs == null || bufs.length == 0 ) |
| 29 | + throw new Exception( "buffer is empty" ); |
| 30 | + |
| 31 | + this.bufs = bufs; |
| 32 | + for ( ByteBuffer b : bufs ) |
| 33 | + b.flip(); |
| 34 | + } |
| 35 | + |
| 36 | + public int read() { |
| 37 | + do { |
| 38 | + if ( bufs[currentBuf].hasRemaining() ) |
| 39 | + return bufs[currentBuf].get(); |
| 40 | + currentBuf++; |
| 41 | + } |
| 42 | + while ( currentBuf < bufs.length ); |
| 43 | + |
| 44 | + currentBuf--; |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + public int read( byte[] buf ) { |
| 49 | + int len = buf.length; |
| 50 | + int bufPos = 0; |
| 51 | + do { |
| 52 | + if ( bufs[currentBuf].hasRemaining() ) { |
| 53 | + int n = Math.min( bufs[currentBuf].remaining(), len-bufPos ); |
| 54 | + bufs[currentBuf].get( buf, bufPos, n ); |
| 55 | + bufPos += n; |
| 56 | + } |
| 57 | + currentBuf++; |
| 58 | + } |
| 59 | + while ( currentBuf < bufs.length && bufPos < len ); |
| 60 | + |
| 61 | + currentBuf--; |
| 62 | + |
| 63 | + if ( bufPos > 0 || ( bufPos == 0 && len == 0 ) ) |
| 64 | + return bufPos; |
| 65 | + else |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + public String readLine() throws IOException { |
| 70 | + byte[] b = new byte[1]; |
| 71 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 72 | + boolean eol = false; |
| 73 | + |
| 74 | + while ( read( b, 0, 1 ) != -1 ) { |
| 75 | + if ( b[0] == 13 ) { |
| 76 | + eol = true; |
| 77 | + } |
| 78 | + else { |
| 79 | + if ( eol ) { |
| 80 | + if ( b[0] == 10 ) |
| 81 | + break; |
| 82 | + eol = false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // cast byte into char array |
| 87 | + bos.write( b, 0, 1 ); |
| 88 | + } |
| 89 | + |
| 90 | + if ( bos == null || bos.size() <= 0 ) { |
| 91 | + throw new IOException( "++++ Stream appears to be dead, so closing it down" ); |
| 92 | + } |
| 93 | + |
| 94 | + // else return the string |
| 95 | + return bos.toString().trim(); |
| 96 | + } |
| 97 | + |
| 98 | + public void clearEOL() throws IOException { |
| 99 | + byte[] b = new byte[1]; |
| 100 | + boolean eol = false; |
| 101 | + while ( read( b, 0, 1 ) != -1 ) { |
| 102 | + |
| 103 | + // only stop when we see |
| 104 | + // \r (13) followed by \n (10) |
| 105 | + if ( b[0] == 13 ) { |
| 106 | + eol = true; |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if ( eol ) { |
| 111 | + if ( b[0] == 10 ) |
| 112 | + break; |
| 113 | + eol = false; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public String toString() { |
| 119 | + StringBuilder sb = new StringBuilder( "ByteBufArrayIS: " ); |
| 120 | + sb.append( bufs.length ).append( " bufs of sizes: \n" ); |
| 121 | + |
| 122 | + for ( int i=0; i < bufs.length; i++ ) { |
| 123 | + sb.append( " " ) |
| 124 | + .append (i ).append( ": " ).append( bufs[i] ).append( "\n" ); |
| 125 | + } |
| 126 | + return sb.toString(); |
| 127 | + } |
| 128 | +} |
0 commit comments