1 18 19 package com.knowgate.jcifs.smb; 20 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 24 import com.knowgate.debug.*; 25 26 class TransactNamedPipeInputStream extends InputStream { 27 28 private static final int INIT_PIPE_SIZE = 4096; 29 30 private SmbNamedPipe pipe; 31 private byte[] pipe_buf = new byte[INIT_PIPE_SIZE]; 32 private int beg_idx, nxt_idx, used; 33 34 Object lock; 35 36 TransactNamedPipeInputStream( SmbNamedPipe pipe ) { 37 this.pipe = pipe; 38 lock = new Object (); 39 } 40 public void close() throws IOException { 41 pipe.close(); 42 } 43 public int read() throws IOException { 44 int result = -1; 45 46 synchronized( lock ) { 47 try { 48 while( used == 0 ) { 49 lock.wait(); 50 } 51 } catch( InterruptedException ie ) { 52 throw new IOException ( ie.getMessage() ); 53 } 54 result = pipe_buf[beg_idx] & 0xFF; 55 beg_idx = ( beg_idx + 1 ) % pipe_buf.length; 56 } 57 return result; 58 } 59 public int read( byte[] b ) throws IOException { 60 return read( b, 0, b.length ); 61 } 62 public int read( byte[] b, int off, int len ) throws IOException { 63 int result = -1; 64 int i; 65 66 if( len <= 0 ) { 67 return 0; 68 } 69 synchronized( lock ) { 70 try { 71 while( used == 0 ) { 72 lock.wait(); 73 } 74 } catch( InterruptedException ie ) { 75 throw new IOException ( ie.getMessage() ); 76 } 77 i = pipe_buf.length - beg_idx; 78 result = len > used ? used : len; 79 if( used > i && result > i ) { 80 System.arraycopy( pipe_buf, beg_idx, b, off, i ); 81 off += i; 82 System.arraycopy( pipe_buf, 0, b, off, result - i ); 83 } else { 84 System.arraycopy( pipe_buf, beg_idx, b, off, result ); 85 } 86 used -= result; 87 beg_idx = ( beg_idx + result ) % pipe_buf.length; 88 } 89 return result; 90 } 91 public int available() throws IOException { 92 if( DebugFile.trace ) 93 DebugFile.writeln( "Named Pipe available() does not apply to TRANSACT Named Pipes" ); 94 return 0; 95 } 96 int receive( byte[] b, int off, int len ) { 97 int i; 98 99 if( len > ( pipe_buf.length - used )) { 100 byte[] tmp; 101 int new_size; 102 103 new_size = pipe_buf.length * 2; 104 if( len > ( new_size - used )) { 105 new_size = len + used; 106 } 107 tmp = pipe_buf; 108 pipe_buf = new byte[new_size]; 109 i = tmp.length - beg_idx; 110 if( used > i ) { 111 System.arraycopy( tmp, beg_idx, pipe_buf, 0, i ); 112 System.arraycopy( tmp, 0, pipe_buf, i, used - i ); 113 nxt_idx = used; 114 } else { 115 System.arraycopy( tmp, beg_idx, pipe_buf, 0, used ); 116 } 117 beg_idx = 0; 118 tmp = null; 119 } 120 121 i = pipe_buf.length - nxt_idx; 122 if( len > i ) { 123 System.arraycopy( b, off, pipe_buf, nxt_idx, i ); 124 off += i; 125 System.arraycopy( b, off, pipe_buf, 0, len - i ); 126 } else { 127 System.arraycopy( b, off, pipe_buf, nxt_idx, len ); 128 } 129 nxt_idx = ( nxt_idx + len ) % pipe_buf.length; 130 used += len; 131 return len; 132 } 133 } 134 | Popular Tags |