File: SmbTableFile.java

package info (click to toggle)
jcifs 1.3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,860 kB
  • ctags: 3,997
  • sloc: java: 23,131; xml: 3,672; ansic: 386; sh: 171; makefile: 27
file content (128 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* jcifs smb client library in Java
 * Copyright (C) 2003  "Michael B. Allen" <jcifs at samba dot org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import jcifs.smb.*;
import java.io.*;

public class SmbTableFile extends SmbRandomAccessFile {

    static final byte BYTE_FULL = (byte)0xFF;

    byte[] hdr = new byte[512];
    byte[] buf = new byte[1024];
    char[] cbuf = new char[512];
    int recordSize, row;

    public SmbTableFile( SmbFile file, String mode, int recordSize ) throws IOException {
        super( file, mode );
        this.recordSize = recordSize;
        read( hdr, 0, 512 );
    }
    public SmbTableFile( String url, String mode, int shareAccess, int recordSize ) throws IOException {
        super( url, mode, shareAccess );
        this.recordSize = recordSize;
        read( hdr, 0, 512 );
    }

    public void insert( SmbTableFileRecord tfr ) throws IOException {
        int i, b = 0;

        /* Find an unset bit it in the bitmap
         */
        for( i = 128; i < 512; i++ ) {
            if( hdr[i] != BYTE_FULL ) {
                /* bitwise complement inverts each bit
                 * mask with 0xFF ensures we only use 8 bits of int b
                 */
                b = ~hdr[i] & 0xFF;
                /* clever trick to isolate first bit on
                 */
                b = b & -b;
                break;
            }
        }
        if( i == 512 ) {
            throw new IOException( "No more space in " + this );
        }
        /* convert power of two to position
         */
        switch( b ) {
            case 1: b = 0; break;
            case 2: b = 1; break;
            case 4: b = 2; break;
            case 8: b = 3; break;
            case 16: b = 4; break;
            case 32: b = 5; break;
            case 64: b = 6; break;
            case 128: b = 7; break;
        }
        tfr.rowid = (i - 128) * 8 + b;
        update( tfr );
    }
    public void update( SmbTableFileRecord tfr ) throws IOException {
        int i;

        seek( 512L + tfr.rowid * recordSize );
        tfr.encode( this );

        i = 128 + tfr.rowid / 8;
        seek( i );
        hdr[i] |= 1 << (tfr.rowid % 8);
        write( hdr[i] );
    }
    public void get( SmbTableFileRecord tfr ) throws IOException {
        seek( 512L + tfr.rowid * recordSize );
        tfr.decode( this );
    }
    public void iterate() {
        row = 0;
    }
    public boolean next( SmbTableFileRecord tfr ) throws IOException {
        int i, b;

        i = 128 + row / 8;       /* Search bitmap for next bit that is on */
        b = 1 << (row % 8);
        for( ; i < 512; i++ ) {
            if(( hdr[i] & -b ) != 0 ) {
                b = hdr[i] & -b;
                b = b & -b;
                break;
            }
            b = 1;
        }
        if( i == 512 ) {                   /* Are no more on bits, return */
            return false;
        }
        switch( b ) {
            case 1: b = 0; break;
            case 2: b = 1; break;
            case 4: b = 2; break;
            case 8: b = 3; break;
            case 16: b = 4; break;
            case 32: b = 5; break;
            case 64: b = 6; break;
            case 128: b = 7; break;
        }
        tfr.rowid = (i - 128) * 8 + b;               /* Set rowid and get */
        get( tfr );

        row = tfr.rowid + 1;               /* Iterate row for next caller */

        return true;
    }
}