1 18 19 package jcifs.smb; 20 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 import java.net.UnknownHostException ; 24 import java.net.MalformedURLException ; 25 26 class TransactNamedPipeInputStream extends SmbFileInputStream { 27 28 private static final int INIT_PIPE_SIZE = 4096; 29 30 private byte[] pipe_buf = new byte[INIT_PIPE_SIZE]; 31 private int beg_idx, nxt_idx, used; 32 private boolean dcePipe; 33 34 Object lock; 35 36 TransactNamedPipeInputStream( SmbNamedPipe pipe ) throws SmbException, 37 MalformedURLException , UnknownHostException { 38 super( pipe, ( pipe.pipeType & 0xFFFF0000 ) | SmbFile.O_EXCL ); 39 this.dcePipe = ( pipe.pipeType & SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT ) != SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT; 40 lock = new Object (); 41 } 42 public int read() throws IOException { 43 int result = -1; 44 45 synchronized( lock ) { 46 try { 47 while( used == 0 ) { 48 lock.wait(); 49 } 50 } catch( InterruptedException ie ) { 51 throw new IOException ( ie.getMessage() ); 52 } 53 result = pipe_buf[beg_idx] & 0xFF; 54 beg_idx = ( beg_idx + 1 ) % pipe_buf.length; 55 } 56 return result; 57 } 58 public int read( byte[] b ) throws IOException { 59 return read( b, 0, b.length ); 60 } 61 public int read( byte[] b, int off, int len ) throws IOException { 62 int result = -1; 63 int i; 64 65 if( len <= 0 ) { 66 return 0; 67 } 68 synchronized( lock ) { 69 try { 70 while( used == 0 ) { 71 lock.wait(); 72 } 73 } catch( InterruptedException ie ) { 74 throw new IOException ( ie.getMessage() ); 75 } 76 i = pipe_buf.length - beg_idx; 77 result = len > used ? used : len; 78 if( used > i && result > i ) { 79 System.arraycopy( pipe_buf, beg_idx, b, off, i ); 80 off += i; 81 System.arraycopy( pipe_buf, 0, b, off, result - i ); 82 } else { 83 System.arraycopy( pipe_buf, beg_idx, b, off, result ); 84 } 85 used -= result; 86 beg_idx = ( beg_idx + result ) % pipe_buf.length; 87 } 88 89 return result; 90 } 91 public int available() throws IOException { 92 if( file.log.level > 2 ) 93 file.log.println( "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 } else { 114 System.arraycopy( tmp, beg_idx, pipe_buf, 0, used ); 115 } 116 beg_idx = 0; 117 nxt_idx = used; 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 public int dce_read( byte[] b, int off, int len ) throws IOException { 134 return super.read(b, off, len); 135 } 136 } 137 | Popular Tags |