1 18 19 package jcifs.netbios; 20 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 24 class SocketInputStream extends InputStream { 25 26 private static final int TMP_BUFFER_SIZE = 256; 27 28 private InputStream in; 29 private SessionServicePacket ssp; 30 private int tot, bip, n; 31 private byte[] header, tmp; 32 33 SocketInputStream( InputStream in ) { 34 this.in = in; 35 header = new byte[4]; 36 tmp = new byte[TMP_BUFFER_SIZE]; 37 } 38 39 public synchronized int read() throws IOException { 40 if( read( tmp, 0, 1 ) < 0 ) { 41 return -1; 42 } 43 return tmp[0] & 0xFF; 44 } 45 public synchronized int read( byte[] b ) throws IOException { 46 return read( b, 0, b.length ); 47 } 48 49 52 53 public synchronized int read( byte[] b, int off, int len ) throws IOException { 54 if( len == 0 ) { 55 return 0; 56 } 57 tot = 0; 58 59 while( true ) { 60 while( bip > 0 ) { 61 n = in.read( b, off, Math.min( len, bip )); 62 if( n == -1 ) { 63 return tot > 0 ? tot : -1; 64 } 65 tot += n; 66 off += n; 67 len -= n; 68 bip -= n; 69 if( len == 0 ) { 70 return tot; 71 } 72 } 73 74 switch( SessionServicePacket.readPacketType( in, header, 0 )) { 75 case SessionServicePacket.SESSION_KEEP_ALIVE: 76 break; 77 case SessionServicePacket.SESSION_MESSAGE: 78 bip = SessionServicePacket.readLength( header, 0 ); 79 break; 80 case -1: 81 if( tot > 0 ) { 82 return tot; 83 } 84 return -1; 85 } 86 } 87 } 88 public synchronized long skip( long numbytes ) throws IOException { 89 if( numbytes <= 0 ) { 90 return 0; 91 } 92 long n = numbytes; 93 while( n > 0 ) { 94 int r = read( tmp, 0, (int)Math.min( (long)TMP_BUFFER_SIZE, n )); 95 if (r < 0) { 96 break; 97 } 98 n -= r; 99 } 100 return numbytes - n; 101 } 102 public int available() throws IOException { 103 if( bip > 0 ) { 104 return bip; 105 } 106 return in.available(); 107 } 108 public void close() throws IOException { 109 in.close(); 110 } 111 } 112 113 | Popular Tags |