1 18 19 package com.knowgate.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 import com.knowgate.debug.*; 28 29 32 33 public class SmbFileInputStream extends InputStream { 34 35 private SmbFile file; 36 private long fp; 37 private int readSize, openFlags; 38 private byte[] tmp = new byte[1]; 39 40 49 50 public SmbFileInputStream( String url ) throws SmbException, MalformedURLException , UnknownHostException { 51 this( new SmbFile( url )); 52 } 53 54 63 64 public SmbFileInputStream( SmbFile file ) throws SmbException, MalformedURLException , UnknownHostException { 65 this( file, SmbFile.O_RDONLY ); 66 } 67 68 SmbFileInputStream( SmbFile file, int openFlags ) throws SmbException, MalformedURLException , UnknownHostException { 69 this.file = file; 70 this.openFlags = openFlags; 71 file.open( openFlags, SmbFile.ATTR_NORMAL, 0 ); 72 readSize = Math.min( file.tree.session.transport.rcv_buf_size - 70, 73 file.tree.session.transport.server.maxBufferSize - 70 ); 74 } 75 76 81 82 public void close() throws IOException { 83 file.close(); 84 } 85 86 91 92 public int read() throws IOException { 93 if( read( tmp, 0, 1 ) == -1 ) { 95 return -1; 96 } 97 return tmp[0] & 0xFF; 98 } 99 100 105 106 public int read( byte[] b ) throws IOException { 107 return read( b, 0, b.length ); 108 } 109 110 115 116 public int read( byte[] b, int off, int len ) throws IOException { 117 if( len <= 0 ) { 118 return 0; 119 } 120 long start = fp; 121 122 file.open( openFlags, SmbFile.ATTR_NORMAL, 0 ); 124 125 128 129 if( DebugFile.trace ) 130 DebugFile.writeln( "read: fid=" + file.fid + ",off=" + off + ",len=" + len ); 131 132 SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off ); 133 134 if( file.type == SmbFile.TYPE_NAMED_PIPE ) { 135 response.responseTimeout = 0; 136 } 137 138 int r, n; 139 do { 140 r = len > readSize ? readSize : len; 141 142 if( DebugFile.trace ) 143 DebugFile.writeln( "read: len=" + len + ",r=" + r + ",fp=" + fp ); 144 145 try { 146 file.send( new SmbComReadAndX( file.fid, fp, r, null ), response ); 147 } catch( SmbException se ) { 148 if( file.type == SmbFile.TYPE_NAMED_PIPE && 149 se.getNtStatus() == NtStatus.NT_STATUS_PIPE_BROKEN ) { 150 return -1; 151 } 152 throw se; 153 } 154 if(( n = response.dataLength ) <= 0 ) { 155 return (int)((fp - start) > 0L ? fp - start : -1); 156 } 157 fp += n; 158 len -= n; 159 response.off += n; 160 } while( len > 0 && n == r ); 161 162 return (int)(fp - start); 163 } 164 public int available() throws IOException { 165 SmbNamedPipe pipe; 166 TransPeekNamedPipe req; 167 TransPeekNamedPipeResponse resp; 168 169 if( file.type != SmbFile.TYPE_NAMED_PIPE ) { 170 return 0; 171 } 172 173 pipe = (SmbNamedPipe)file; 174 file.open(( pipe.pipeType & 0xFF0000 ) | SmbFile.O_EXCL, SmbFile.ATTR_NORMAL, 0 ); 175 176 req = new TransPeekNamedPipe( file.unc, file.fid ); 177 resp = new TransPeekNamedPipeResponse( pipe ); 178 179 pipe.sendTransaction( req, resp ); 180 if( resp.status == TransPeekNamedPipeResponse.STATUS_DISCONNECTED || 181 resp.status == TransPeekNamedPipeResponse.STATUS_SERVER_END_CLOSED ) { 182 file.opened = false; 183 return 0; 184 } 185 return resp.available; 186 } 187 193 public long skip( long n ) throws IOException { 194 if (n > 0) { 195 fp += n; 196 return n; 197 } 198 return 0; 199 } 200 } 201 202 | Popular Tags |