SmbRandomAccessFilepublic class SmbRandomAccessFile extends Object implements DataInput, DataOutput
Fields Summary |
---|
private static final int | WRITE_OPTIONS | private SmbFile | file | private long | fp | private int | openFlags | private int | access | private int | readSize | private int | writeSize | private int | ch | private int | options | private byte[] | tmp | private SmbComWriteAndXResponse | write_andx_resp |
Constructors Summary |
---|
public SmbRandomAccessFile(String url, String mode, int shareAccess)
this( new SmbFile( url, "", null, shareAccess ), mode );
| public SmbRandomAccessFile(SmbFile file, String mode)
this.file = file;
if( mode.equals( "r" )) {
this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDONLY;
} else if( mode.equals( "rw" )) {
this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDWR | SmbFile.O_APPEND;
write_andx_resp = new SmbComWriteAndXResponse();
options = WRITE_OPTIONS;
access = SmbConstants.FILE_READ_DATA | SmbConstants.FILE_WRITE_DATA;
} else {
throw new IllegalArgumentException( "Invalid mode" );
}
file.open( openFlags, access, SmbFile.ATTR_NORMAL, options );
readSize = file.tree.session.transport.rcv_buf_size - 70;
writeSize = file.tree.session.transport.snd_buf_size - 70;
fp = 0L;
|
Methods Summary |
---|
public void | close()
file.close();
| public long | getFilePointer()
return fp;
| public long | length()
return file.length();
| public int | read()
if( read( tmp, 0, 1 ) == -1 ) {
return -1;
}
return tmp[0] & 0xFF;
| public int | read(byte[] b)
return read( b, 0, b.length );
| public int | read(byte[] b, int off, int len)
if( len <= 0 ) {
return 0;
}
long start = fp;
// ensure file is open
if( file.isOpen() == false ) {
file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
}
int r, n;
SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off );
do {
r = len > readSize ? readSize : len;
file.send( new SmbComReadAndX( file.fid, fp, r, null ), response );
if(( n = response.dataLength ) <= 0 ) {
return (int)((fp - start) > 0L ? fp - start : -1);
}
fp += n;
len -= n;
response.off += n;
} while( len > 0 && n == r );
return (int)(fp - start);
| public final boolean | readBoolean()
if((read( tmp, 0, 1 )) < 0 ) {
throw new SmbException( "EOF" );
}
return tmp[0] != (byte)0x00;
| public final byte | readByte()
if((read( tmp, 0, 1 )) < 0 ) {
throw new SmbException( "EOF" );
}
return tmp[0];
| public final char | readChar()
if((read( tmp, 0, 2 )) < 0 ) {
throw new SmbException( "EOF" );
}
return (char)Encdec.dec_uint16be( tmp, 0 );
| public final double | readDouble()
if((read( tmp, 0, 8 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_doublebe( tmp, 0 );
| public final float | readFloat()
if((read( tmp, 0, 4 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_floatbe( tmp, 0 );
| public final void | readFully(byte[] b)
readFully( b, 0, b.length );
| public final void | readFully(byte[] b, int off, int len)
int n = 0, count;
do {
count = this.read( b, off + n, len - n );
if( count < 0 ) throw new SmbException( "EOF" );
n += count;
fp += count;
} while( n < len );
| public final int | readInt()
if((read( tmp, 0, 4 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_uint32be( tmp, 0 );
| public final java.lang.String | readLine()
StringBuffer input = new StringBuffer();
int c = -1;
boolean eol = false;
while (!eol) {
switch( c = read() ) {
case -1:
case '\n":
eol = true;
break;
case '\r":
eol = true;
long cur = fp;
if( read() != '\n" ) {
fp = cur;
}
break;
default:
input.append( (char)c );
break;
}
}
if ((c == -1) && (input.length() == 0)) {
return null;
}
return input.toString();
| public final long | readLong()
if((read( tmp, 0, 8 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_uint64be( tmp, 0 );
| public final short | readShort()
if((read( tmp, 0, 2 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_uint16be( tmp, 0 );
| public final java.lang.String | readUTF()
int size = readUnsignedShort();
byte[] b = new byte[size];
read( b, 0, size );
try {
return Encdec.dec_utf8( b, 0, size );
} catch( IOException ioe ) {
throw new SmbException( "", ioe );
}
| public final int | readUnsignedByte()
if((read( tmp, 0, 1 )) < 0 ) {
throw new SmbException( "EOF" );
}
return tmp[0] & 0xFF;
| public final int | readUnsignedShort()
if((read( tmp, 0, 2 )) < 0 ) {
throw new SmbException( "EOF" );
}
return Encdec.dec_uint16be( tmp, 0 ) & 0xFFFF;
| public void | seek(long pos)
fp = pos;
| public void | setLength(long newLength)
// ensure file is open
if( file.isOpen() == false ) {
file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
}
SmbComWriteResponse rsp = new SmbComWriteResponse();
file.send( new SmbComWrite( file.fid, (int)(newLength & 0xFFFFFFFFL), 0, tmp, 0, 0 ), rsp );
| public int | skipBytes(int n)
if (n > 0) {
fp += n;
return n;
}
return 0;
| public void | write(byte[] b)
write( b, 0, b.length );
| public void | write(byte[] b, int off, int len)
if( len <= 0 ) {
return;
}
// ensure file is open
if( file.isOpen() == false ) {
file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
}
int w;
do {
w = len > writeSize ? writeSize : len;
file.send( new SmbComWriteAndX( file.fid, fp, len - w, b, off, w, null ), write_andx_resp );
fp += write_andx_resp.count;
len -= write_andx_resp.count;
off += write_andx_resp.count;
} while( len > 0 );
| public void | write(int b)
tmp[0] = (byte)b;
write( tmp, 0, 1 );
| public final void | writeBoolean(boolean v)
tmp[0] = (byte)(v ? 1 : 0);
write( tmp, 0, 1 );
| public final void | writeByte(int v)
tmp[0] = (byte)v;
write( tmp, 0, 1 );
| public final void | writeBytes(java.lang.String s)
byte[] b = s.getBytes();
write( b, 0, b.length );
| public final void | writeChar(int v)
Encdec.enc_uint16be( (short)v, tmp, 0 );
write( tmp, 0, 2 );
| public final void | writeChars(java.lang.String s)
int clen = s.length();
int blen = 2 * clen;
byte[] b = new byte[blen];
char[] c = new char[clen];
s.getChars( 0, clen, c, 0 );
for( int i = 0, j = 0; i < clen; i++ ) {
b[j++] = (byte)(c[i] >>> 8);
b[j++] = (byte)(c[i] >>> 0);
}
write( b, 0, blen );
| public final void | writeDouble(double v)
Encdec.enc_doublebe( v, tmp, 0 );
write( tmp, 0, 8 );
| public final void | writeFloat(float v)
Encdec.enc_floatbe( v, tmp, 0 );
write( tmp, 0, 4 );
| public final void | writeInt(int v)
Encdec.enc_uint32be( v, tmp, 0 );
write( tmp, 0, 4 );
| public final void | writeLong(long v)
Encdec.enc_uint64be( v, tmp, 0 );
write( tmp, 0, 8 );
| public final void | writeShort(int v)
Encdec.enc_uint16be( (short)v, tmp, 0 );
write( tmp, 0, 2 );
| public final void | writeUTF(java.lang.String str)
int len = str.length();
int ch, size = 0;
byte[] dst;
for( int i = 0; i < len; i++ ) {
ch = str.charAt( i );
size += ch > 0x07F ? (ch > 0x7FF ? 3 : 2) : 1;
}
dst = new byte[size];
writeShort( size );
try {
Encdec.enc_utf8( str, dst, 0, size );
} catch( IOException ioe ) {
throw new SmbException( "", ioe );
}
write( dst, 0, size );
|
|