1 18 19 package jcifs.smb; 20 21 import java.io.DataInput ; 22 import java.io.DataOutput ; 23 import java.io.DataInputStream ; 24 import java.io.DataOutputStream ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.UnknownHostException ; 28 import jcifs.util.Encdec; 29 30 public class SmbRandomAccessFile implements DataOutput , DataInput { 31 32 private static final int WRITE_OPTIONS = 0x0842; 33 34 private SmbFile file; 35 private long fp; 36 private int openFlags, readSize, writeSize, ch, options = 0; 37 private byte[] tmp = new byte[8]; 38 private SmbComWriteAndXResponse write_andx_resp = null; 39 40 public SmbRandomAccessFile( String url, String mode, int shareAccess ) 41 throws SmbException, MalformedURLException , UnknownHostException { 42 this( new SmbFile( url, "", null, shareAccess ), mode ); 43 } 44 public SmbRandomAccessFile( SmbFile file, String mode ) 45 throws SmbException, MalformedURLException , UnknownHostException { 46 this.file = file; 47 if( mode.equals( "r" )) { 48 this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDONLY; 49 } else if( mode.equals( "rw" )) { 50 this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDWR | SmbFile.O_APPEND; 51 write_andx_resp = new SmbComWriteAndXResponse(); 52 options = WRITE_OPTIONS; 53 } else { 54 throw new IllegalArgumentException ( "Invalid mode" ); 55 } 56 file.open( openFlags, SmbFile.ATTR_NORMAL, options ); 57 readSize = file.tree.session.transport.rcv_buf_size - 70; 58 writeSize = file.tree.session.transport.snd_buf_size - 70; 59 fp = 0L; 60 } 61 62 public int read() throws SmbException { 63 if( read( tmp, 0, 1 ) == -1 ) { 64 return -1; 65 } 66 return tmp[0] & 0xFF; 67 } 68 public int read( byte b[] ) throws SmbException { 69 return read( b, 0, b.length ); 70 } 71 public int read( byte b[], int off, int len ) throws SmbException { 72 if( len <= 0 ) { 73 return 0; 74 } 75 long start = fp; 76 77 if( file.isOpen() == false ) { 79 file.open( openFlags, SmbFile.ATTR_NORMAL, options ); 80 } 81 82 int r, n; 83 SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off ); 84 do { 85 r = len > readSize ? readSize : len; 86 file.send( new SmbComReadAndX( file.fid, fp, r, null ), response ); 87 if(( n = response.dataLength ) <= 0 ) { 88 return (int)((fp - start) > 0L ? fp - start : -1); 89 } 90 fp += n; 91 len -= n; 92 response.off += n; 93 } while( len > 0 && n == r ); 94 95 return (int)(fp - start); 96 } 97 public final void readFully( byte b[] ) throws SmbException { 98 readFully( b, 0, b.length ); 99 } 100 public final void readFully( byte b[], int off, int len ) throws SmbException { 101 int n = 0, count; 102 103 do { 104 count = this.read( b, off + n, len - n ); 105 if( count < 0 ) throw new SmbException( "EOF" ); 106 n += count; 107 fp += count; 108 } while( n < len ); 109 } 110 public int skipBytes( int n ) throws SmbException { 111 if (n > 0) { 112 fp += n; 113 return n; 114 } 115 return 0; 116 } 117 118 public void write( int b ) throws SmbException { 119 tmp[0] = (byte)b; 120 write( tmp, 0, 1 ); 121 } 122 public void write( byte b[] ) throws SmbException { 123 write( b, 0, b.length ); 124 } 125 public void write( byte b[], int off, int len ) throws SmbException { 126 if( len <= 0 ) { 127 return; 128 } 129 130 if( file.isOpen() == false ) { 132 file.open( openFlags, SmbFile.ATTR_NORMAL, options ); 133 } 134 135 int w; 136 do { 137 w = len > writeSize ? writeSize : len; 138 file.send( new SmbComWriteAndX( file.fid, fp, len - w, b, off, w, null ), write_andx_resp ); 139 fp += write_andx_resp.count; 140 len -= write_andx_resp.count; 141 off += write_andx_resp.count; 142 } while( len > 0 ); 143 } 144 public long getFilePointer() throws SmbException { 145 return fp; 146 } 147 public void seek( long pos ) throws SmbException { 148 fp = pos; 149 } 150 public long length() throws SmbException { 151 return file.length(); 152 } 153 public void setLength( long newLength ) throws SmbException { 154 if( file.isOpen() == false ) { 156 file.open( openFlags, SmbFile.ATTR_NORMAL, options ); 157 } 158 SmbComWriteResponse rsp = new SmbComWriteResponse(); 159 file.send( new SmbComWrite( file.fid, (int)(newLength & 0xFFFFFFFFL), 0, tmp, 0, 0 ), rsp ); 160 } 161 public void close() throws SmbException { 162 file.close(); 163 } 164 165 public final boolean readBoolean() throws SmbException { 166 if((read( tmp, 0, 1 )) < 0 ) { 167 throw new SmbException( "EOF" ); 168 } 169 return tmp[0] != (byte)0x00; 170 } 171 public final byte readByte() throws SmbException { 172 if((read( tmp, 0, 1 )) < 0 ) { 173 throw new SmbException( "EOF" ); 174 } 175 return tmp[0]; 176 } 177 public final int readUnsignedByte() throws SmbException { 178 if((read( tmp, 0, 1 )) < 0 ) { 179 throw new SmbException( "EOF" ); 180 } 181 return tmp[0] & 0xFF; 182 } 183 public final short readShort() throws SmbException { 184 if((read( tmp, 0, 2 )) < 0 ) { 185 throw new SmbException( "EOF" ); 186 } 187 return Encdec.dec_uint16be( tmp, 0 ); 188 } 189 public final int readUnsignedShort() throws SmbException { 190 if((read( tmp, 0, 2 )) < 0 ) { 191 throw new SmbException( "EOF" ); 192 } 193 return Encdec.dec_uint16be( tmp, 0 ); 194 } 195 public final char readChar() throws SmbException { 196 if((read( tmp, 0, 2 )) < 0 ) { 197 throw new SmbException( "EOF" ); 198 } 199 return (char)Encdec.dec_uint16be( tmp, 0 ); 200 } 201 public final int readInt() throws SmbException { 202 if((read( tmp, 0, 4 )) < 0 ) { 203 throw new SmbException( "EOF" ); 204 } 205 return Encdec.dec_uint32be( tmp, 0 ); 206 } 207 public final long readLong() throws SmbException { 208 if((read( tmp, 0, 8 )) < 0 ) { 209 throw new SmbException( "EOF" ); 210 } 211 return Encdec.dec_uint64be( tmp, 0 ); 212 } 213 public final float readFloat() throws SmbException { 214 if((read( tmp, 0, 4 )) < 0 ) { 215 throw new SmbException( "EOF" ); 216 } 217 return Encdec.dec_floatbe( tmp, 0 ); 218 } 219 public final double readDouble() throws SmbException { 220 if((read( tmp, 0, 8 )) < 0 ) { 221 throw new SmbException( "EOF" ); 222 } 223 return Encdec.dec_doublebe( tmp, 0 ); 224 } 225 public final String readLine() throws SmbException { 226 StringBuffer input = new StringBuffer (); 227 int c = -1; 228 boolean eol = false; 229 230 while (!eol) { 231 switch( c = read() ) { 232 case -1: 233 case '\n': 234 eol = true; 235 break; 236 case '\r': 237 eol = true; 238 long cur = fp; 239 if( read() != '\n' ) { 240 fp = cur; 241 } 242 break; 243 default: 244 input.append( (char)c ); 245 break; 246 } 247 } 248 249 if ((c == -1) && (input.length() == 0)) { 250 return null; 251 } 252 253 return input.toString(); 254 } 255 256 public final String readUTF() throws SmbException { 257 int size = readUnsignedShort(); 258 byte[] b = new byte[size]; 259 read( b, 0, size ); 260 try { 261 return Encdec.dec_utf8( b, 0, size ); 262 } catch( IOException ioe ) { 263 throw new SmbException( "", ioe ); 264 } 265 } 266 public final void writeBoolean( boolean v ) throws SmbException { 267 tmp[0] = (byte)(v ? 1 : 0); 268 write( tmp, 0, 1 ); 269 } 270 public final void writeByte( int v ) throws SmbException { 271 tmp[0] = (byte)v; 272 write( tmp, 0, 1 ); 273 } 274 public final void writeShort( int v ) throws SmbException { 275 Encdec.enc_uint16be( (short)v, tmp, 0 ); 276 write( tmp, 0, 2 ); 277 } 278 public final void writeChar( int v ) throws SmbException { 279 Encdec.enc_uint16be( (short)v, tmp, 0 ); 280 write( tmp, 0, 2 ); 281 } 282 public final void writeInt( int v ) throws SmbException { 283 Encdec.enc_uint32be( v, tmp, 0 ); 284 write( tmp, 0, 4 ); 285 } 286 public final void writeLong( long v ) throws SmbException { 287 Encdec.enc_uint64be( v, tmp, 0 ); 288 write( tmp, 0, 8 ); 289 } 290 public final void writeFloat( float v ) throws SmbException { 291 Encdec.enc_floatbe( v, tmp, 0 ); 292 write( tmp, 0, 4 ); 293 } 294 public final void writeDouble( double v ) throws SmbException { 295 Encdec.enc_doublebe( v, tmp, 0 ); 296 write( tmp, 0, 8 ); 297 } 298 public final void writeBytes( String s ) throws SmbException { 299 byte[] b = s.getBytes(); 300 write( b, 0, b.length ); 301 } 302 public final void writeChars( String s ) throws SmbException { 303 int clen = s.length(); 304 int blen = 2 * clen; 305 byte[] b = new byte[blen]; 306 char[] c = new char[clen]; 307 s.getChars( 0, clen, c, 0 ); 308 for( int i = 0, j = 0; i < clen; i++ ) { 309 b[j++] = (byte)(c[i] >>> 8); 310 b[j++] = (byte)(c[i] >>> 0); 311 } 312 write( b, 0, blen ); 313 } 314 public final void writeUTF( String str ) throws SmbException { 315 int len = str.length(); 316 int ch, size = 0; 317 byte[] dst; 318 319 for( int i = 0; i < len; i++ ) { 320 ch = str.charAt( i ); 321 size += ch > 0x07F ? (ch > 0x7FF ? 3 : 2) : 1; 322 } 323 dst = new byte[size]; 324 writeShort( size ); 325 try { 326 Encdec.enc_utf8( str, dst, 0, size ); 327 } catch( IOException ioe ) { 328 throw new SmbException( "", ioe ); 329 } 330 write( dst, 0, size ); 331 } 332 } 333 334 | Popular Tags |