1 18 19 package jcifs.smb; 20 21 import java.net.URL ; 22 import java.net.UnknownHostException ; 23 import java.net.MalformedURLException ; 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 27 30 31 public class SmbFileInputStream extends InputStream { 32 33 private long fp; 34 private int readSize, openFlags, access; 35 private byte[] tmp = new byte[1]; 36 37 SmbFile file; 38 39 47 48 public SmbFileInputStream( String url ) throws SmbException, MalformedURLException , UnknownHostException { 49 this( new SmbFile( url )); 50 } 51 52 60 61 public SmbFileInputStream( SmbFile file ) throws SmbException, MalformedURLException , UnknownHostException { 62 this( file, SmbFile.O_RDONLY ); 63 } 64 65 SmbFileInputStream( SmbFile file, int openFlags ) throws SmbException, MalformedURLException , UnknownHostException { 66 this.file = file; 67 this.openFlags = openFlags & 0xFFFF; 68 this.access = (openFlags >>> 16) & 0xFFFF; 69 if (file.type != SmbFile.TYPE_NAMED_PIPE) { 70 file.open( openFlags, access, SmbFile.ATTR_NORMAL, 0 ); 71 this.openFlags &= ~(SmbFile.O_CREAT | SmbFile.O_TRUNC); 72 } else { 73 file.connect0(); 74 } 75 readSize = Math.min( file.tree.session.transport.rcv_buf_size - 70, 76 file.tree.session.transport.server.maxBufferSize - 70 ); 77 } 78 79 84 85 public void close() throws IOException { 86 file.close(); 87 tmp = null; 88 } 89 90 95 96 public int read() throws IOException { 97 if( read( tmp, 0, 1 ) == -1 ) { 99 return -1; 100 } 101 return tmp[0] & 0xFF; 102 } 103 104 109 110 public int read( byte[] b ) throws IOException { 111 return read( b, 0, b.length ); 112 } 113 114 119 120 public int read( byte[] b, int off, int len ) throws IOException { 121 return readDirect(b, off, len); 122 } 123 public int readDirect( byte[] b, int off, int len ) throws IOException { 124 if( len <= 0 ) { 125 return 0; 126 } 127 long start = fp; 128 129 if( tmp == null ) { 130 throw new IOException ( "Bad file descriptor" ); 131 } 132 file.open( openFlags, access, SmbFile.ATTR_NORMAL, 0 ); 134 135 138 139 if( file.log.level >= 4 ) 140 file.log.println( "read: fid=" + file.fid + ",off=" + off + ",len=" + len ); 141 142 SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off ); 143 144 if( file.type == SmbFile.TYPE_NAMED_PIPE ) { 145 response.responseTimeout = 0; 146 } 147 148 int r, n; 149 do { 150 r = len > readSize ? readSize : len; 151 152 if( file.log.level >= 4 ) 153 file.log.println( "read: len=" + len + ",r=" + r + ",fp=" + fp ); 154 155 try { 156 SmbComReadAndX request = new SmbComReadAndX( file.fid, fp, r, null ); 157 if( file.type == SmbFile.TYPE_NAMED_PIPE ) { 158 request.minCount = request.maxCount = request.remaining = 1024; 159 } 160 file.send( request, response ); 161 } catch( SmbException se ) { 162 if( file.type == SmbFile.TYPE_NAMED_PIPE && 163 se.getNtStatus() == NtStatus.NT_STATUS_PIPE_BROKEN ) { 164 return -1; 165 } 166 throw se; 167 } 168 if(( n = response.dataLength ) <= 0 ) { 169 return (int)((fp - start) > 0L ? fp - start : -1); 170 } 171 fp += n; 172 len -= n; 173 response.off += n; 174 } while( len > 0 && n == r ); 175 176 return (int)(fp - start); 177 } 178 185 public int available() throws IOException { 186 SmbNamedPipe pipe; 187 TransPeekNamedPipe req; 188 TransPeekNamedPipeResponse resp; 189 190 if( file.type != SmbFile.TYPE_NAMED_PIPE ) { 191 return 0; 192 } 193 194 pipe = (SmbNamedPipe)file; 195 file.open(SmbFile.O_EXCL, pipe.pipeType & 0xFF0000, SmbFile.ATTR_NORMAL, 0 ); 196 197 req = new TransPeekNamedPipe( file.unc, file.fid ); 198 resp = new TransPeekNamedPipeResponse( pipe ); 199 200 pipe.send( req, resp ); 201 if( resp.status == TransPeekNamedPipeResponse.STATUS_DISCONNECTED || 202 resp.status == TransPeekNamedPipeResponse.STATUS_SERVER_END_CLOSED ) { 203 file.opened = false; 204 return 0; 205 } 206 return resp.available; 207 } 208 214 public long skip( long n ) throws IOException { 215 if (n > 0) { 216 fp += n; 217 return n; 218 } 219 return 0; 220 } 221 } 222 223 | Popular Tags |