KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SmbTableFile


1 /* jcifs smb client library in Java
2  * Copyright (C) 2003 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 import jcifs.smb.*;
20 import java.io.*;
21
22 public class SmbTableFile extends SmbRandomAccessFile {
23
24     static final byte BYTE_FULL = (byte)0xFF;
25
26     byte[] hdr = new byte[512];
27     byte[] buf = new byte[1024];
28     char[] cbuf = new char[512];
29     int recordSize, row;
30
31     public SmbTableFile( SmbFile file, String JavaDoc mode, int recordSize ) throws IOException {
32         super( file, mode );
33         this.recordSize = recordSize;
34         read( hdr, 0, 512 );
35     }
36     public SmbTableFile( String JavaDoc url, String JavaDoc mode, int shareAccess, int recordSize ) throws IOException {
37         super( url, mode, shareAccess );
38         this.recordSize = recordSize;
39         read( hdr, 0, 512 );
40     }
41
42     public void insert( SmbTableFileRecord tfr ) throws IOException {
43         int i, b = 0;
44
45         /* Find an unset bit it in the bitmap
46          */

47         for( i = 128; i < 512; i++ ) {
48             if( hdr[i] != BYTE_FULL ) {
49                 /* bitwise complement inverts each bit
50                  * mask with 0xFF ensures we only use 8 bits of int b
51                  */

52                 b = ~hdr[i] & 0xFF;
53                 /* clever trick to isolate first bit on
54                  */

55                 b = b & -b;
56                 break;
57             }
58         }
59         if( i == 512 ) {
60             throw new IOException( "No more space in " + this );
61         }
62         /* convert power of two to position
63          */

64         switch( b ) {
65             case 1: b = 0; break;
66             case 2: b = 1; break;
67             case 4: b = 2; break;
68             case 8: b = 3; break;
69             case 16: b = 4; break;
70             case 32: b = 5; break;
71             case 64: b = 6; break;
72             case 128: b = 7; break;
73         }
74         tfr.rowid = (i - 128) * 8 + b;
75         update( tfr );
76     }
77     public void update( SmbTableFileRecord tfr ) throws IOException {
78         int i;
79
80         seek( 512L + tfr.rowid * recordSize );
81         tfr.encode( this );
82
83         i = 128 + tfr.rowid / 8;
84         seek( i );
85         hdr[i] |= 1 << (tfr.rowid % 8);
86         write( hdr[i] );
87     }
88     public void get( SmbTableFileRecord tfr ) throws IOException {
89         seek( 512L + tfr.rowid * recordSize );
90         tfr.decode( this );
91     }
92     public void iterate() {
93         row = 0;
94     }
95     public boolean next( SmbTableFileRecord tfr ) throws IOException {
96         int i, b;
97
98         i = 128 + row / 8; /* Search bitmap for next bit that is on */
99         b = 1 << (row % 8);
100         for( ; i < 512; i++ ) {
101             if(( hdr[i] & -b ) != 0 ) {
102                 b = hdr[i] & -b;
103                 b = b & -b;
104                 break;
105             }
106             b = 1;
107         }
108         if( i == 512 ) { /* Are no more on bits, return */
109             return false;
110         }
111         switch( b ) {
112             case 1: b = 0; break;
113             case 2: b = 1; break;
114             case 4: b = 2; break;
115             case 8: b = 3; break;
116             case 16: b = 4; break;
117             case 32: b = 5; break;
118             case 64: b = 6; break;
119             case 128: b = 7; break;
120         }
121         tfr.rowid = (i - 128) * 8 + b; /* Set rowid and get */
122         get( tfr );
123
124         row = tfr.rowid + 1; /* Iterate row for next caller */
125
126         return true;
127     }
128 }
129
Popular Tags