KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestRandomAccess


1 import jcifs.smb.*;
2
3 public class TestRandomAccess {
4
5     public static class TestRecord extends SmbTableFileRecord {
6
7         boolean f1; /* 1 byte */
8         byte f2; /* 1 byte */
9         int f3; /* 1 byte */
10         short f4; /* 2 bytes */
11         int f5; /* 2 bytes */
12         char f6; /* 2 bytes */
13         int f7; /* 4 bytes */
14         long f8; /* 8 bytes */
15         float f9; /* 4 bytes */
16         double f10; /* 8 bytes */
17         String JavaDoc f11; /* 95 bytes max */
18                     /* 128 bytes total */
19
20         public TestRecord() {
21         }
22         public TestRecord( boolean f1, byte f2, int f3, short f4,
23                 int f5, char f6, int f7, long f8,
24                 float f9, double f10, String JavaDoc f11 ) {
25             this.f1 = f1;
26             this.f2 = f2;
27             this.f3 = f3;
28             this.f4 = f4;
29             this.f5 = f5;
30             this.f6 = f6;
31             this.f7 = f7;
32             this.f8 = f8;
33             this.f9 = f9;
34             this.f10 = f10;
35             this.f11 = f11;
36         }
37
38         public void encode( SmbTableFile tf ) throws SmbException {
39             tf.writeBoolean( f1 );
40             tf.writeByte( f2 );
41             tf.writeByte( f3 );
42             tf.writeShort( f4 );
43             tf.writeShort( f5 );
44             tf.writeChar( f6 );
45             tf.writeInt( f7 );
46             tf.writeLong( f8 );
47             tf.writeFloat( f9 );
48             tf.writeDouble( f10 );
49             tf.writeUTF( f11 );
50         }
51         public void decode( SmbTableFile tf ) throws SmbException {
52             f1 = tf.readBoolean();
53             f2 = tf.readByte();
54             f3 = tf.readUnsignedByte();
55             f4 = tf.readShort();
56             f5 = tf.readUnsignedShort();
57             f6 = tf.readChar();
58             f7 = tf.readInt();
59             f8 = tf.readLong();
60             f9 = tf.readFloat();
61             f10 = tf.readDouble();
62             f11 = tf.readUTF();
63         }
64         public boolean equals( Object JavaDoc obj ) {
65             if( obj instanceof TestRecord ) {
66                 TestRecord r = (TestRecord)obj;
67
68                 return r.f1 == f1 &&
69                     r.f2 == f2 &&
70                     r.f3 == f3 &&
71                     r.f4 == f4 &&
72                     r.f5 == f5 &&
73                     r.f6 == f6 &&
74                     r.f7 == f7 &&
75                     r.f8 == f8 &&
76                     r.f9 == f9 &&
77                     r.f10 == f10 &&
78                     f11.equals( r.f11 );
79             }
80             return false;
81         }
82     }
83
84     public static void main( String JavaDoc[] argv ) throws Exception JavaDoc {
85         if( argv.length < 2 ) {
86             System.err.println( "usage: TestRandomAccess <url> <N> (1 for read or 2 for write with <index>)" );
87             return;
88         }
89         SmbTableFile stf;
90         int op = Integer.parseInt( argv[1] );
91
92         TestRecord r1 = new TestRecord( true, (byte)0x12, 0x34, (short)0x1122,
93                     0x3344, '\u04c1', 0x11112222, 0x1111111122222222L,
94                     0.1122f, 3344.1, "The surface is smooth like glass" );
95
96         if( op == 3 ) {
97             stf = new SmbTableFile( argv[0], "rw", 0, 128 );
98             int newLength = Integer.parseInt( argv[2] );
99             stf.setLength( newLength );
100             System.out.println( "truncated to " + newLength );
101         } else if( op == 1 ) {
102             SmbFile file = new SmbFile( argv[0], null, SmbFile.FILE_SHARE_READ );
103             stf = new SmbTableFile( file, "rw", 128 );
104             stf.insert( r1 );
105             System.out.println( "rowid: " + r1.rowid );
106         } else {
107             if( argv.length < 3 ) {
108                 System.err.println( "usage: TestRandomAccess <url> <N> (1 for read or 2 for write with <index>)" );
109                 return;
110             }
111             stf = new SmbTableFile( argv[0], "r", 0, 128 );
112             TestRecord r2 = new TestRecord();
113             r2.rowid = Integer.parseInt( argv[2] );
114             stf.get( r2 );
115             System.out.println( "r1.equals( r2 ) = " + r1.equals( r2 ));
116         }
117
118         stf.close();
119     }
120 }
121
122
Popular Tags