File: GrowthByteBuffer.java

package info (click to toggle)
libjlha-java 0.0.20050504-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,364 kB
  • ctags: 1,338
  • sloc: java: 10,782; xml: 139; makefile: 14
file content (394 lines) | stat: -rw-r--r-- 13,063 bytes parent folder | download | duplicates (6)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//start of GrowthByteBuffer.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF

/**
 * GrowthByteBuffer.java
 * 
 * Copyright (C) 2001-2002  Michel Ishizuka  All rights reserved.
 * 
 * ȉ̏ɓӂȂ΃\[XƃoCi`̍ĔzzƎgp
 * ύX̗Lɂ炸‚B
 * 
 * PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
 *     щL̐ێȂĂ͂ȂȂB
 * 
 * QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
 *     щL̐gp ̑̔zz
 *     ܂ގɋLqȂ΂ȂȂB
 * 
 * ̃\tgEFA͐Β˔ڂɂĖۏ؂Œ񋟂A̖
 * IBłƂۏ؁AilLƂۏ؂ɂƂǂ܂炸A
 * Ȃ閾IшÎIȕۏ؂ȂB
 * Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
 * IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
 * AƖ̒f〈܂Ăv̈⎸A֐i
 * T[rX̓l邪AĂꂾɌ肳Ȃ
 * Q)ɑ΂āAȂ鎖Ԃ̌ƂȂƂĂA_̐
 * C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
 * ŝׂ߂łƂĂA܂͂̂悤ȑQ̉”\
 * ĂƂĂ؂̐ӔC𕉂Ȃ̂ƂB
 */

package jp.gr.java_conf.dangan.io;

//import classes and interfaces

//import exceptions
import java.lang.IllegalArgumentException;


/**
 * IɐLobt@B<br>
 * RandomAccessFile  łƂĎgpB
 * A܂苐ȃf[^舵̂ɂ͌ȂB
 * XbhZ[tł͂ȂB
 * jdk1.4 ȍ~ ByteBufferƂ͌݊B
 * 
 * <pre>
 * -- revision history --
 * $Log: GrowthByteBuffer.java,v $
 * Revision 1.1  2002/12/05 00:00:00  dangan
 * [maintenance]
 *     \[X
 *
 * Revision 1.0  2002/07/24 00:00:00  dangan
 * add to version control
 * [bug fix]
 *     grow() Ńobt@̑ʂ̌vZԈĂ̂CB
 * [change]
 *     ǂݍ݌EɒB read( new byte[0] )  
 *     read( byte[] buf, int off, 0 ) ̖߂l
 *     InputStream Ɠ 0 ɂȂ悤ɂ
 * [maintenance]
 *     \[X
 *
 * </pre>
 * 
 * @author  $Author: dangan $
 * @version $Revision: 1.1 $
 */
public class GrowthByteBuffer{


    //------------------------------------------------------------------
    //  class field
    //------------------------------------------------------------------
    //  default
    //------------------------------------------------------------------
    //  private static final int DefaultBufferSize
    //------------------------------------------------------------------
    /**
     * ftHg̈‚̃obt@̃TCY
     */
    private static final int DefaultBufferSize = 16384;


    //------------------------------------------------------------------
    //  instance field
    //------------------------------------------------------------------
    //  byte buffer
    //------------------------------------------------------------------
    //  private byte[][] buffer
    //  private int position
    //  private int limit
    //------------------------------------------------------------------
    /**
     * obt@
     * S buffer[0].length ƓTCYbytez̔zB
     */
    private byte[][] buffer;

    /**
     * ݏʒuB
     * position  limitȍ~ɂȂ”\B
     */
    private int position;

    /**
     * ݓǂ݂݌EB
     * ȍ~̃f[^͕sB
     * ̈ʒũf[^͓ǂ߂邱Ƃɒӂ邱ƁB
     */
    private int limit;


    //------------------------------------------------------------------
    //  constructer
    //------------------------------------------------------------------
    //  public GrowthByteBuffer()
    //  public GrouthByteBuffer( int BufferSize )
    //------------------------------------------------------------------
    /**
     * TCYŐLobt@\zB<br>
     * obt@TCYɂ̓ftHglgpB
     */
    public GrowthByteBuffer(){
        this( GrowthByteBuffer.DefaultBufferSize );
    }

    /**
     * TCYŐLobt@\zB<br>
     *
     * @param BufferSize obt@̃TCY
     */
    public GrowthByteBuffer( int BufferSize ){
        if( 0 < BufferSize ){
            this.buffer    = new byte[16][];
            this.buffer[0] = new byte[ BufferSize ];
            this.position  = 0;
            this.limit     = -1;
        }else{
            throw new IllegalArgumentException( "BufferSize most be 1 or more." );
        }
    }


    //------------------------------------------------------------------
    //  original method
    //------------------------------------------------------------------
    //  write
    //------------------------------------------------------------------
    //  public void write( int data )
    //  public void write( byte[] buffer )
    //  public void write( byte[] buffer, int index, int length )
    //------------------------------------------------------------------
    /**
     * ݈ʒu 1oCg̃f[^ށB
     * 
     * @param data 1oCg̃f[^
     */
    public void write( int data ){
        this.grow( this.position );

        this.buffer[ this.position / this.buffer[0].length ]
                   [ this.position % this.buffer[0].length ]
            = (byte)data;

        this.position++;
    }

    /**
     * ݈ʒu buffer ̓eށB
     * 
     * @param buffer ރf[^يi[ꂽobt@
     */
    public void write( byte[] buffer ){
        this.write( buffer, 0, buffer.length );
    }

    /**
     * ݈ʒu buffer  indexlengthoCg̓eށB
     * 
     * @param buffer ރf[^يi[ꂽobt@
     * @param index  buffeȑރf[^̊Jnʒu
     * @param length ރf[^
     */
    public void write( byte[] buffer, int index, int length ){
        this.grow( this.position + length - 1 );

        while( 0 < length ){
            int copylen = Math.min( ( this.position / this.buffer[0].length + 1 )
                                         * this.buffer[0].length,
                                    this.position + length ) - this.position;

            System.arraycopy( buffer, index, 
                              this.buffer[ this.position / this.buffer[0].length ], 
                                           this.position % this.buffer[0].length,
                              copylen );

            this.position += copylen;
            index         += copylen;
            length        -= copylen;
        }
    }


    //------------------------------------------------------------------
    //  original method
    //------------------------------------------------------------------
    //  read
    //------------------------------------------------------------------
    //  public int read()
    //  public int read( byte[] buffer )
    //  public int read( byte[] buffer, int index, int length )
    //------------------------------------------------------------------
    /**
     * ݈ʒu 1bytẽf[^ǂ݂ށB
     * 
     * @return ǂ݂܂ꂽ1bytẽf[^B<br>
     *         ǂ݂݌E𒴂ēǂƂꍇ -1
     */
    public int read(){
        if( this.position <= this.limit ){
            return this.buffer[ this.position / this.buffer[0].length ]
                              [ this.position++ % this.buffer[0].length ] & 0xFF;
        }else{
            return -1;
        }
    }

    /**
     * ݈ʒu buffer𖞂悤Ƀf[^ǂݍށB
     * 
     * @param buffer f[^ǂݍރobt@
     * 
     * @return ۂɓǂ݂܂ꂽf[^<br>
     *         ǂ݂݌E𒴂ēǂƂꍇ -1
     */
    public int read( byte[] buffer ){
        return this.read( buffer, 0, buffer.length );
    }

    /**
     * ݈ʒu buffer index length̃f[^ǂݍށB
     * 
     * @param buffer f[^ǂݍރobt@
     * @param index  bufferf[^ǂ݂݈ʒu
     * @param length ǂݍރf[^̗
     * 
     * @return ۂɓǂ݂܂ꂽf[^<br>
     *         ǂ݂݌E𒴂ēǂƂꍇ -1
     */
    public int read( byte[] buffer, int index, int length ){
        if( this.position <= this.limit ){
            int len = 0;
            while( 0 < length ){
                int copylen = Math.min( Math.min( ( this.position / this.buffer[0].length + 1 )
                                                  * this.buffer[0].length,
                                                  this.position + length ),
                                        this.limit + 1 ) - this.position;
                if( 0 < copylen ){
                    System.arraycopy( this.buffer[ this.position / this.buffer[0].length ], 
                                                   this.position % this.buffer[0].length,
                                      buffer, index, 
                                     copylen );

                    this.position += copylen;
                    index         += copylen;
                    len           += copylen;
                    length        -= copylen;
                }else{
                    break;
                }
            }
            return len;
        }else if( 0 < length ){
            return -1;
        }else{
            return 0;
        }
    }


    //------------------------------------------------------------------
    //  original methods
    //------------------------------------------------------------------
    //  access methods
    //------------------------------------------------------------------
    //  public int length()
    //  public void setLength( int length )
    //  public int position()
    //  public void setPosition( int position )
    //  public void seek( int position )
    //------------------------------------------------------------------
    /**
     * ݂̓ǂ݂݌E𓾂B
     * 
     * @return ݂̓ǂ݂݌E
     */
    public int length(){
        return this.limit + 1;
    }

    /**
     * ǂ݂݌Eʒuݒ肷B
     * 
     * @param Vǂ݂݌Eʒu
     */
    public void setLength( int length ){
        length--;
        if( this.limit < length ){
            this.grow( length );
        }else{
            this.limit = length;
        }
    }

    /**
     * ݈ʒu𓾂B
     * 
     * @return ݈ʒu
     */
    public int position(){
        return this.position;
    }

    /**
     * ݈ʒuݒ肷B
     * java.io.RandomAccessFileƓ 
     * setPosition œǂ݂݌E𒴂l
     * ݒ肵ɂ̓obt@͑ĂȂB
     * ̌ write ɂď񂾎ɂ͂
     * ߂ăobt@͑B
     * 
     * @param position V݈ʒu
     */
    public void setPosition( int position ){
        this.position = position;
    }

    /**
     * ݈ʒuݒ肷B
     * java.io.RandomAccessFileƓ 
     * seek œǂ݂݌E𒴂l
     * ݒ肵ɂ̓obt@͑ĂȂB
     * ̌ write ɂď񂾎ɂ͂
     * ߂ăobt@͑B
     * 
     * @param position V݈ʒu
     */
    public void seek( int position ){
        this.setPosition( position );
    }


    //------------------------------------------------------------------
    //  local method
    //------------------------------------------------------------------
    //  private void grow( int limit )
    //------------------------------------------------------------------
    /**
     * Vǂ݂݌E limit ݒ肵A
     * limit ܂ obt@𑝉B
     * 
     * @param Vǂ݂݌E
     */
    private void grow( int limit ){
        if( this.limit < limit ){
            int last = 0;
            while( last < this.buffer.length 
                && this.buffer[last] != null )
                last++;

            limit++;
            if( last * this.buffer[0].length < limit ){
                int need = ( limit / this.buffer[0].length )
                         + ( limit % this.buffer[0].length == 0 ? 0 : 1 );

                if( this.buffer.length < need ){
                    byte[][] old = this.buffer;
                    this.buffer = new byte[ Math.max( old.length * 2, need ) ][];

                    for( int i = 0 ; i < last ; i++ )
                        this.buffer[i] = old[i];
                }
                for( int i = last ; i < need ; i++ )
                    this.buffer[ i ] = new byte[ this.buffer[0].length ];
            }

            this.limit = limit - 1;
        }
    }

}
//end of GrowthByteBuffer.java