File: BitInputStream.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 (1038 lines) | stat: -rw-r--r-- 40,497 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
//start of BitInputStream.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF

/**
 * BitInputStream.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 java.io.InputStream;

//import exceptions
import java.io.IOException;
import java.io.EOFException;
import java.lang.NullPointerException;
import java.lang.IllegalArgumentException;
import jp.gr.java_conf.dangan.io.BitDataBrokenException;
import jp.gr.java_conf.dangan.io.NotEnoughBitsException;

/**
 * rbg͂̂߂̃[eBeBNXB<br>
 * 
 * <pre>
 * -- revision history --
 * $Log: BitInputStream.java,v $
 * Revision 1.5  2002/12/07 00:00:00  dangan
 * [maintenance]
 *     \[X
 *
 * Revision 1.4  2002/11/15 00:00:00  dangan
 * [improvement]
 *     prefetchBits()   32bit ̓ǂݍ݂ۏ؂悤ɏC
 * [change]
 *     \bh̕ύX
 *     prefetchBit     -> peekBit
 *     prefetchBoolean -> peekBoolean
 *     prefetchBits    -> peekBits
 *
 * Revision 1.3  2002/11/02 00:00:00  dangan
 * [bug fix]
 *     available() availableBits() 
 *     ubNɓǂݍ߂ʂ傫lԂĂB
 *
 * Revision 1.2  2002/09/05 00:00:00  dangan
 * [change]
 *     EndOfStream ɒB read( new byte[0] )  
 *     read( byte[] buf, int off, 0 ) ̖߂l
 *     InputStream Ɠ 0 ɂȂ悤ɂ
 *
 * Revision 1.1  2002/09/04 00:00:00  dangan
 * [bug fix]
 *     skip( len )  skipBits( len )  len  0 ̂Ƃ
 *     łĂȂB
 *
 * Revision 1.0  2002/09/03 00:00:00  dangan
 * add to version control
 * [bug fix]
 *     mark()  ڑꂽ in ɓn readLimit ̌vZÂ߁A
 *     vꂽ readLimit ɒBOɃ}[Nʒuj鎖B
 *     EndOfStream ɒB skip()  skip( 0 )  -1 ԂĂB
 * [maintenance]
 *     ^up~
 *     CZX̏C
 *
 *
 * </pre>
 * 
 * @author  $Author: dangan $
 * @version $Revision: 1.5 $
 */
public class BitInputStream extends InputStream{

    //------------------------------------------------------------------
    //  class field
    //------------------------------------------------------------------
    //  default
    //------------------------------------------------------------------
    //  private static final int DefaultCacheSize
    //------------------------------------------------------------------
    /**
     * ftHg̃LbVTCY
     */
    private static final int DefaultCacheSize = 1024;


    //------------------------------------------------------------------
    //  instance field
    //------------------------------------------------------------------
    //  source
    //------------------------------------------------------------------
    //  private InputStream in
    //------------------------------------------------------------------
    /**
     * ڑꂽ̓Xg[
     */
    private InputStream in;


    //------------------------------------------------------------------
    //  instance field
    //------------------------------------------------------------------
    //  cache
    //------------------------------------------------------------------
    //  private byte[] cache
    //  private int    cacheLimit
    //  private int    cachePosition
    //------------------------------------------------------------------
    /**
     * xቺ}~poCgz
     */
    private byte[] cache;

    /**
     * cache ̗LoCg
     */
    private int    cacheLimit;

    /**
     * cache ̌ݏʒu
     */
    private int    cachePosition;


    //------------------------------------------------------------------
    //  instance field
    //------------------------------------------------------------------
    //  bit buffer
    //------------------------------------------------------------------
    //  private int    bitBuffer
    //  private int    bitCount
    //------------------------------------------------------------------
    /**
     * rbgobt@B
     * rbgf[^͍ŏʃrbg bitCount i[ĂB
     */
    private int    bitBuffer;

    /**
     * bitBuffer  Lrbg
     */
    private int    bitCount;


    //------------------------------------------------------------------
    //  instance field
    //------------------------------------------------------------------
    //  backup for mark/reset
    //------------------------------------------------------------------
    //  private boolean markPositionIsInCache
    //  private byte[] markCache
    //  private int    markCacheLimit
    //  private int    markCachePosition
    //  private int    markBitBuffer
    //  private int    markBitCount
    //------------------------------------------------------------------
    /**
     * markʒuLbV͈͓̔ɂ邩B
     * markꂽƂ true ɐݒ肳A
     *  in  LbVւ̓ǂݍ݂
     * sꂽƂ false ɐݒ肳B
     */
    private boolean markPositionIsInCache;

    /** cache  obNAbvp */
    private byte[] markCache;

    /** cacheLimit ̃obNAbvp */
    private int    markCacheLimit;

    /** cachePosition ̃obNAbvp */
    private int    markCachePosition;

    /** bitBuffer ̃obNAbvp */
    private int    markBitBuffer;

    /** bitCount ̃obNAbvp */
    private int    markBitCount;


    //------------------------------------------------------------------
    //  constructer
    //------------------------------------------------------------------
    //  private BitInputStream()
    //  public BitInputStream( InputStream in )
    //  public BitInputStream( InputStream in, int CacheSize )
    //------------------------------------------------------------------
    /**
     * ftHgRXgN^B
     * gpsB
     */
    private BitInputStream(){ }

    /**
     * ̓Xg[ in ̃f[^rbgPʂ
     * ǂݍ߂悤ȃXg[\zB<br>
     * 
     * @param in ̓Xg[
     */
    public BitInputStream( InputStream in ){
        this( in, BitInputStream.DefaultCacheSize );
    }

    /**
     * ̓Xg[ in ̃f[^rbgPʂ
     * ǂݍ߂悤ȃXg[\zB<br>
     * 
     * @param in        ̓Xg[
     * @param CacheSize obt@TCY
     */
    public BitInputStream( InputStream in, int CacheSize ){
        if( in != null && 4 <= CacheSize ){
            this.in                    = in;
            this.cache                 = new byte[ CacheSize ];
            this.cacheLimit            = 0;
            this.cachePosition         = 0;
            this.bitBuffer             = 0;
            this.bitCount              = 0;

            this.markPositionIsInCache = false;
            this.markCache             = null;
            this.markCacheLimit        = 0;
            this.markCachePosition     = 0;
            this.markBitBuffer         = 0;
            this.markBitCount          = 0;
        }else if( in == null ){
            throw new NullPointerException( "in" );
        }else{
            throw new IllegalArgumentException( "CacheSize must be 4 or more." );
        }
    }


    //------------------------------------------------------------------
    //  method of java.io.InputStream
    //------------------------------------------------------------------
    //  read
    //------------------------------------------------------------------
    //  public int read()
    //  public int read( byte[] buffer )
    //  public int read( byte[] buffer, int index, int length )
    //  public long skip( long length )
    //------------------------------------------------------------------
    /**
     * ڑꂽXg[ 8rbg̃f[^ǂݍށB<br>
     * 
     * @return ǂݏoꂽ 8rbg̃f[^B<br>
     *          EndOfStream ɒBĂꍇ -1
     * 
     * @exception IOException
     *               ڑꂽ̓Xg[
     *               o̓G[ꍇ
     * @exception BitDataBrokenException 
     *               EndOfStreamɒB
     *               vꂽrbg̃f[^
     *               ǂݍ݂ɎsꍇB<br>
     */
    public int read() throws IOException {
        try{
            return this.readBits( 8 );                                          //throws LocalEOFException BitDataBrokenException IOException
        }catch( LocalEOFException exception ){
            if( exception.thrownBy( this ) ) return -1;
            else                             throw exception;
        }
    }

    /**
     * ڑꂽ̓Xg[ oCgz buffer 
     * 悤Ƀf[^ǂݍށB<br>
     * f[^͕K buffer 𖞂Ƃ͌ȂƂɒӁB<br>
     * 
     * @param buffer ǂݍ܂ꂽf[^i[邽߂̃oCgz
     * 
     * @return buffer ɓǂݍ񂾃f[^ʂoCgŕԂB<br>
     *          EndOfStream ɒBĂꍇ -1 ԂB<br>
     * 
     * @exception IOException
     *               ڑꂽ̓Xg[
     *               o̓G[ꍇ
     * @exception BitDataBrokenException 
     *               EndOfStreamɒB
     *               vꂽrbg̃f[^
     *               ǂݍ݂ɎsꍇB<br>
     */
    public int read( byte[] buffer ) throws IOException {
        return this.read( buffer, 0, buffer.length );                           //throws BitDataBrokenException IOException
    }

    /**
     * ڑꂽ̓Xg[ oCgz buffer 
     * index Ŏw肳ꂽʒu length oCg̃f[^
     * ǂݍށB<br>
     * ̃\bh lengthoCgǂݍނA
     * EndOfStream ɓB܂ŃubNB<br>
     * f[^͕K length oCgǂݍ܂Ƃ͌
     * ȂƂɒӁB<br>
     * 
     * @param buffer ǂݍ܂ꂽf[^i[邽߂̃oCgz
     * @param index  buffer̃f[^ǂݍ݊Jnʒu
     * @param length bufferɓǂݍރf[^
     * 
     * @return buffer ɓǂݍ񂾃f[^ʂoCgŕԂB<br>
     *          EndOfStream ɒBĂꍇ -1 ԂB<br>
     * 
     * @exception IOException
     *               ڑꂽ̓Xg[
     *               o̓G[ꍇ
     * @exception BitDataBrokenException 
     *               EndOfStreamɒB
     *               vꂽrbg̃f[^
     *               ǂݍ݂ɎsꍇB<br>
     */
    public int read( byte[] buffer, int index, int length ) throws IOException {
        final int requested = length;
        try{
            while( 0 < length ){
                buffer[index++] = (byte)this.readBits( 8 );                     //throws LocalEOFException BitDataBrokenException IOException
                length--;
            }
            return requested;
        }catch( LocalEOFException exception ){
            if( exception.thrownBy( this ) ){
                if( requested != length ) return requested - length;
                else                      return -1;
            }else{
                throw exception;
            }
        }catch( BitDataBrokenException exception ){
            if( exception.getCause() instanceof LocalEOFException 
             && ((LocalEOFException)exception.getCause()).thrownBy( this ) ){
                this.bitBuffer >>>= exception.getBitCount();
                this.bitCount  +=   exception.getBitCount();
                this.bitBuffer |= exception.getBitData() <<
                                    ( 32 - exception.getBitCount() );

                return requested - length;
            }else{
                throw exception;
            }
        }
    }

    /**
     * ڑꂽ̓Xg[̃f[^ length oCg
     * ǂݔ΂B<br>
     * ̃\bh lengthoCgǂݔ΂A
     * EndOfStream ɓB܂ŃubNB<br>
     * f[^͕K length oCgǂݔ΂Ƃ͌
     * ȂƂɒӁB<br>
     * 
     * @param length ǂݔ΂oCgB<br>
     * 
     * @return ۂɓǂݔ΂ꂽoCgB<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public long skip( long length ) throws IOException {
        length = ( 0 < length ? length : 0 );
        final long requested = length;
        try{
            while( 0 < length ){
                this.readBits( 8 );
                length--;
            }
            return requested;
        }catch( LocalEOFException exception ){
            return requested - length;
        }catch( BitDataBrokenException exception ){
            if( exception.getCause() instanceof LocalEOFException 
             && ((LocalEOFException)exception.getCause()).thrownBy( this ) ){
                this.bitBuffer >>>= exception.getBitCount();
                this.bitCount  +=   exception.getBitCount();
                this.bitBuffer |=   exception.getBitData() <<
                                      ( 32 - exception.getBitCount() );
                return requested - length;
            }else{
                throw exception;
            }
        }
    }


    //------------------------------------------------------------------
    //  method of java.io.InputStream
    //------------------------------------------------------------------
    //  mark/reset
    //------------------------------------------------------------------
    //  public void mark( int readLimit )
    //  public void reset()
    //  public boolean markSupported()
    //------------------------------------------------------------------
    /**
     * ڑꂽ̓Xg[݈̌ʒuɃ}[Nݒ肵A
     * reset() \bhŃ}[N_ ǂݍ݈ʒu
     * ߂悤ɂB<br>
     * 
     * @param readLimit }[Nʒuɖ߂ẼoCgB
     *                  ̃oCg𒴂ăf[^ǂ
     *                  񂾏ꍇ reset()łȂȂ
     *                  \B<br>
     */
    public void mark( int readLimit ){
        readLimit -= this.cacheLimit - this.cachePosition;
        readLimit -= this.bitCount / 8;
        readLimit += 4;
        readLimit  = ( ( readLimit / this.cache.length ) * this.cache.length
                     + ( readLimit % this.cache.length == 0 ? 0 : this.cache.length ) );

        this.in.mark( readLimit );

        if( this.markCache == null ){
            this.markCache = (byte[])this.cache.clone();
        }else{
            System.arraycopy( this.cache, 0, 
                              this.markCache, 0, 
                              this.cacheLimit );
        }
        this.markCacheLimit        = this.cacheLimit;
        this.markCachePosition     = this.cachePosition;
        this.markBitBuffer         = this.bitBuffer;
        this.markBitCount          = this.bitCount;
        this.markPositionIsInCache = true;
    }

    /**
     * ڑꂽ̓Xg[̓ǂݍ݈ʒuŌ
     * mark() \bhĂяoꂽƂ̈ʒuɐݒ肷B<br>
     * 
     * @exception IOException <br>
     *              (1) BitInputStream  mark ȂĂȂꍇB<br>
     *              (2) ڑꂽ̓Xg[ markSupported()
     *                  false ԂꍇB<br>
     *              (3) ڑꂽ̓Xg[
     *                  o̓G[ꍇB<br>
     *              ̉ꂩB
     */
    public void reset() throws IOException {
        if( this.markPositionIsInCache ){
            this.cachePosition = this.markCachePosition;
            this.bitBuffer     = this.markBitBuffer;
            this.bitCount      = this.markBitCount;
        }else if( !this.in.markSupported() ){
            throw new IOException( "not support mark()/reset()." );
        }else if( this.markCache == null ){ //͖̏Ƀ}[NĂȂƂBRXgN^ markCache  null ɐݒ肳̂𗘗pB 
            throw new IOException( "not marked." );
        }else{
            //in  reset() łȂꍇ
            //ŏ̍s this.in.reset() 
            //IOException 𓊂邱Ƃ҂ĂB
            this.in.reset();                                                    //throws IOException
            System.arraycopy( this.markCache, 0, 
                              this.cache, 0, 
                              this.markCacheLimit );
            this.cacheLimit    = this.markCacheLimit;
            this.cachePosition = this.markCachePosition;
            this.bitBuffer     = this.markBitBuffer;
            this.bitCount      = this.markBitCount;
        }
    }

    /**
     * ڑꂽ̓Xg[ mark()  reset() 
     * T|[g邩𓾂B<br>
     * 
     * @return Xg[ mark()  reset() 
     *         T|[gꍇ trueB<br>
     *         T|[gȂꍇ falseB<br>
     */
    public boolean markSupported(){
        return this.in.markSupported();
    }


    //------------------------------------------------------------------
    //  method of java.io.InputStream
    //------------------------------------------------------------------
    //  other
    //------------------------------------------------------------------
    //  public int available()
    //  public void close()
    //------------------------------------------------------------------
    /**
     * ڑꂽ̓Xg[ubNȂ
     * ǂݍނƂ̂łoCg𓾂B<br>
     * 
     * @return ubNȂœǂݏooCgB<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public int available() throws IOException {
        return this.availableBits() / 8;                                        //throws IOException
    }

    /**
     * ̓̓Xg[‚A
     * gpĂ\[XJB<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public void close() throws IOException {
        this.in.close();                                                        //throws IOException
        this.in                    = null;

        this.cache                 = null;
        this.cacheLimit            = 0;
        this.cachePosition         = 0;
        this.bitBuffer             = 0;
        this.bitCount              = 0;

        this.markCache             = null;
        this.markCacheLimit        = 0;
        this.markCachePosition     = 0;
        this.markBitBuffer         = 0;
        this.markBitCount          = 0;
        this.markPositionIsInCache = false;
    }


    //------------------------------------------------------------------
    //  original method
    //------------------------------------------------------------------
    //  read
    //------------------------------------------------------------------
    //  public int readBit()
    //  public boolean readBoolean()
    //  public int readBits( int count )
    //  public int skipBits( int count )
    //------------------------------------------------------------------
    /**
     * ڑꂽ̓Xg[ 1rbg̃f[^
     * ǂݍށB<br>
     * 
     * @return ǂݍ܂ꂽ1rbg̃f[^B<br>
     *         EndOfStreamɒBĂꍇ -1B<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public int readBit() throws IOException {
        if( 0 < this.bitCount ){
            int bit = this.bitBuffer >>> 31;
            this.bitBuffer <<= 1;
            this.bitCount   -= 1;
            return bit;
        }else{
            try{
                this.fillBitBuffer();
                int bit = this.bitBuffer >>> 31;
                this.bitBuffer <<= 1;
                this.bitCount   -= 1;
                return bit;
            }catch( LocalEOFException exception ){
                if( exception.thrownBy( this ) ){
                    return -1;
                }else{
                    throw exception;
                }
            }
        }
    }

    /**
     * ڑꂽ̓Xg[ 1rbg̃f[^
     * ^UlƂēǂݍށB<br>
     * 
     * @return ǂݍ܂ꂽ1rbg̃f[^ 
     *         1ł trueA0ł false ԂB<br>
     * 
     * @exception EOFException EndOfStreamɒBĂꍇ
     * @exception IOException  ڑꂽ̓Xg[
     *                         o̓G[ꍇ
     */
    public boolean readBoolean() throws IOException {
        if( 0 < this.bitCount ){
            boolean bool = ( this.bitBuffer < 0 );
            this.bitBuffer <<= 1;
            this.bitCount   -= 1;
            return bool;
        }else{
            this.fillBitBuffer();
            boolean bool = ( this.bitBuffer < 0 );
            this.bitBuffer <<= 1;
            this.bitCount   -= 1;
            return bool;
        }
    }

    /**
     * ڑꂽ̓Xg[ count rbg̃f[^
     * ǂݍށB ߂l intlł鎖悤
     * ǂݍނƂ̂ł őLrbg 32rbg
     * 邪Acount 32ȏ̒lݒ肵Ă`FbN
     * 󂯂Ȃ ȏ̒lݒ肵ꍇ rbg
     * f[^ǂݎ̂ĂB<br>
     * Ƃ readBits( 33 ) ƂƂ ܂1rbg
     * f[^ǂݎ̂āǍ 32rbg̃f[^ԂB<br>
     * ܂ count  0ȉ̐ݒ肵ČĂяoꍇA
     * f[^ǂݍޓ𔺂Ȃ ߂l 0A
     * EndOfStream ɒBĂĂ EOFException 
     * Ȃ_ɒӂ邱ƁB<br>
     * 
     * @param count  ǂݍރf[^̃rbg
     * 
     * @return ǂݍ܂ꂽrbgf[^B<br>
     * 
     * @exception IOException 
     *               ڑꂽ̓Xg[
     *               o̓G[ꍇ
     * @exception EOFException 
     *               EndOfStreamɒBĂꍇ
     * @exception BitDataBrokenException 
     *               ǂݍݓr EndOfStreamɒB
     *               vꂽrbg̃f[^̓ǂݍ
     *               ɎsꍇB<br>
     */
    public int readBits( int count ) throws IOException {
        if( 0 < count ){
            if( count <= this.bitCount ){
                int bits = this.bitBuffer >>> ( 32 - count );
                this.bitBuffer <<= count;
                this.bitCount   -= count;
                return bits;
            }else{
                final int requested = count;
                int bits = 0;
                try{
                    this.fillBitBuffer();                                       //throws LocalEOFException IOException
                    while( this.bitCount < count ){
                        count -= this.bitCount;
                        if( count < 32 ){
                            bits |= ( this.bitBuffer >>> ( 32 - this.bitCount ) ) << count;
                        }
                        this.bitBuffer = 0;
                        this.bitCount  = 0;
                        this.fillBitBuffer();                                   //throws LocalEOFException IOException
                    }
                    bits |= this.bitBuffer >>> ( 32 - count );
                    this.bitBuffer <<= count;
                    this.bitCount   -= count;
                    return bits;
                }catch( LocalEOFException exception ){
                    if( exception.thrownBy( this ) && count < requested ){
                        throw new BitDataBrokenException( exception, bits >>> count, requested - count );
                    }else{
                        throw exception;
                    }
                }
            }
        }else{
            return 0;
        }
    }

    /**
     * ڑꂽXg[ count rbg̃f[^
     * ǂݔ΂B<br>
     * 
     * @param count ǂݔ΂Ăقrbg
     * 
     * @return ۂɓǂݔтrbg
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public int skipBits( int count ) throws IOException {
        count = Math.max( count, 0 );

        if( count < this.bitCount ){
            this.bitBuffer <<= count;
            this.bitCount  -=  count;
            return count;
        }else{
            final int requested = count;
            count -= this.bitCount;
            this.bitCount  = 0;
            this.bitBuffer = 0;
            try{
                while( ( this.cacheLimit - this.cachePosition ) * 8 <= count ){
                    count -= ( this.cacheLimit - this.cachePosition ) * 8;
                    this.cachePosition = this.cacheLimit;
                    this.fillCache();
                    if( this.cacheLimit == this.cachePosition ){
                        throw new LocalEOFException( this );
                    }
                }
                this.cachePosition += ( count >> 3 );
                count = count & 0x07;
                if( 0 < count ){
                    this.bitCount  = 8 - count;
                    this.bitBuffer = this.cache[ this.cachePosition++ ] << ( 24 + count );
                    count = 0;
                }
            }catch( LocalEOFException exception ){
            }
            return requested - count;
        }
    }


    //------------------------------------------------------------------
    //  original method
    //------------------------------------------------------------------
    //  prefetch
    //------------------------------------------------------------------
    //  public int peekBit()
    //  public boolean peekBoolean()
    //  public int peekBits( int count )
    //------------------------------------------------------------------
    /**
     * ǂݍ݈ʒuς 1rbg̃f[^ǂ݂B<br>
     * 
     * @return ǂݍ܂ꂽ1rbg̃f[^B<br>
     *         EndOfStreamɒBĂꍇ -1B<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public int peekBit() throws IOException {
        if( 0 < this.bitCount ){
            return this.bitBuffer >>> 31;
        }else{
            try{
                this.fillBitBuffer();                                           //throws LocalEOFException IOException
                return this.bitBuffer >>> 31;
            }catch( LocalEOFException exception ){
                if( exception.thrownBy( this ) ){
                    return -1;
                }else{
                    throw exception;
                }
            }

        }
    }

    /**
     * ǂݍ݈ʒuς 1rbg̃f[^
     * ^UlƂĐǂ݂B<br>
     * 
     * @return ǂݍ܂ꂽ1rbg̃f[^ 
     *         1ł trueA0ł false ԂB<br>
     * 
     * @exception EOFException EndOfStreamɒBĂꍇ
     * @exception IOException  ڑꂽ̓Xg[
     *                         o̓G[ꍇ
     */
    public boolean peekBoolean() throws IOException {
        if( 0 < this.bitCount ){
            return ( this.bitBuffer < 0 );
        }else{
            this.fillBitBuffer();                                               //throws LocalEOFException IOException
            return ( this.bitBuffer < 0 );
        }
    }

    /**
     * ǂݍ݈ʒuς count rbg̃f[^ǂ݂B<br>
     * ߂l int^ł邱Ƃ킩悤
     * őLrbg 32rbgłB<br>
     * EndOfStream t߂āAǂݏo邱ƂۏႳ̂
     * 32rbgłB(rbgobt@̑傫 32rbgł邽)<br>
     *  32rbgȏ̐ǂ݋@\K{ƂȂꍇ
     * ̓sx mark()AreadBits()Areset() JԂA
     * ̃NXgp邱Ƃ߂邱ƁB<br>
     * 
     * @param count ǂݍރrbg
     * 
     * @return ǂ݂ count rbg̃rbgf[^
     * 
     * @exception EOFException
     *                    EndOfStreamɒBĂꍇ
     * @exception IOException
     *                    ڑꂽ̓Xg[
     *                    o̓G[ꍇ
     * @exception NotEnoughBitsException
     *                    count ǂ݉”\Ȕ͈͊Ȍꍇ
     */
    public int peekBits( int count ) throws IOException {
        if( 0 < count ){
            if( count <= this.bitCount ){
                return this.bitBuffer >>> ( 32 - count );
            }else{
                this.fillBitBuffer();                                           //throws LocalEOFException, IOException
                if( count <= this.bitCount ){
                    return this.bitBuffer >>> ( 32 - count );
                }else if( count <= this.cachedBits() ){
                    if( count <= 32 ){
                        int bits = this.bitBuffer;
                        bits |= ( this.cache[ this.cachePosition ] & 0xFF ) 
                                                  >> ( this.bitCount - 24 );
                        return bits >>> ( 32 - count );
                    }else if( count - 32 < this.bitCount ){
                        int bits  = this.bitBuffer << ( count - 32 );;
                        int bcnt = this.bitCount - ( count - 32 );
                        int pos   = this.cachePosition;
                        while( bcnt < 25 ){
                            bits  |= ( this.cache[ pos++ ] & 0xFF ) << ( 24 - bcnt );
                            bcnt += 8; 
                        }
                        if( bcnt < 32 ){
                            bits  |= ( this.cache[ pos ] & 0xFF ) >> ( bcnt - 24 );
                        }
                        return bits;
                    }else{
                        count  -= this.bitCount;
                        count  -= 32;
                        int pos = this.cachePosition + ( count >> 3 );
                        count  &= 0x07;
                        if( 0 < count ){
                            return   (   this.cache[ pos ]              << ( 24 + count ) )
                                   | ( ( this.cache[ pos + 1 ] & 0xFF ) << ( 16 + count ) )
                                   | ( ( this.cache[ pos + 2 ] & 0xFF ) << (  8 + count ) )
                                   | ( ( this.cache[ pos + 3 ] & 0xFF ) << count )
                                   | ( ( this.cache[ pos + 4 ] & 0xFF ) >> (  8 - count ) );
                        }else{
                            return   (   this.cache[ pos ]              << 24 )
                                   | ( ( this.cache[ pos + 1 ] & 0xFF ) << 16 )
                                   | ( ( this.cache[ pos + 2 ] & 0xFF ) <<  8 )
                                   |   ( this.cache[ pos + 3 ] & 0xFF );
                        }
                    }
                }else{
                    throw new NotEnoughBitsException( this.cachedBits() );
                }
            }
        }else{
            return 0;
        }
    }


    //------------------------------------------------------------------
    //  original method
    //------------------------------------------------------------------
    //  other
    //------------------------------------------------------------------
    //  public int availableBits()
    //  private int cachedBits()
    //------------------------------------------------------------------
    /**
     * ڑꂽ̓Xg[ubNȂ
     * ǂݍނƂ̂łrbg𓾂B<br>
     * 
     * @return ubNȂœǂݏorbgB<br>
     * 
     * @exception IOException ڑꂽ̓Xg[
     *                        o̓G[ꍇ
     */
    public int availableBits() throws IOException {
        int avail = ( this.cacheLimit - this.cachePosition )
                  + this.in.available() / this.cache.length * this.cache.length;//throws IOException
        avail += this.bitCount - 32;

        return Math.max( avail, 0 );
    }

    /**
     *  BitInputStream ɒ~Ărbg𓾂B<br>
     * 
     * @return  BitInputStream ɒ~ĂrbgB<br>
     */
    private int cachedBits(){
        return this.bitCount + ( ( this.cacheLimit - this.cachePosition ) << 3 );
    }


    //------------------------------------------------------------------
    //  local method
    //------------------------------------------------------------------
    //  fill
    //------------------------------------------------------------------
    //  private void fillBitBuffer()
    //  private void fillCache()
    //------------------------------------------------------------------
    /**
     * bitBuffer Ƀf[^𖞂B
     * EndOfStream t߂ bitBuffer ɂ
     * 25bit ̃f[^mۂ邱ƂۏႷB
     * 
     * @exception IOException       o̓G[ꍇ
     * @exception LocalEOFException bitBuffer ̏Ԃ EndOfStream ɒBꍇ
     */
    private void fillBitBuffer() throws IOException {
        if( 32 <= this.cachedBits() ){
            if( this.bitCount <= 24 ){
                if( this.bitCount <= 16 ){
                    if( this.bitCount <= 8 ){
                        if( this.bitCount <= 0 ){
                            this.bitBuffer = this.cache[this.cachePosition++] << 24;
                            this.bitCount  = 8;
                        }
                        this.bitBuffer |= ( this.cache[this.cachePosition++] & 0xFF )
                                                            << ( 24 - this.bitCount );
                        this.bitCount  += 8;
                    }
                    this.bitBuffer |= ( this.cache[this.cachePosition++] & 0xFF )
                                                        << ( 24 - this.bitCount );
                    this.bitCount  += 8;
                }
                this.bitBuffer |= ( this.cache[this.cachePosition++] & 0xFF )
                                                    << ( 24 - this.bitCount );
                this.bitCount  += 8;
            }
        }else if( this.bitCount < 25 ){
            if( this.bitCount == 0 ){
                this.bitBuffer = 0;
            }

            int count = Math.min( ( 32 - this.bitCount ) >> 3, 
                                  this.cacheLimit - this.cachePosition );
            while( 0 < count-- ){
                this.bitBuffer |= ( this.cache[this.cachePosition++] & 0xFF )
                                                    << ( 24 - this.bitCount );
                this.bitCount  += 8;
            }
            this.fillCache();                                                   //throws IOException
            if( this.cachePosition < this.cacheLimit ){
                count = Math.min( ( 32 - this.bitCount ) >> 3, 
                                  this.cacheLimit - this.cachePosition );
                while( 0 < count-- ){
                    this.bitBuffer |= ( this.cache[this.cachePosition++] & 0xFF )
                                                        << ( 24 - this.bitCount );
                    this.bitCount  += 8;
                }
            }else if( this.bitCount <= 0 ){
                throw new LocalEOFException( this );
            }
        }
    }

    /**
     * cache ɂȂ cache Ƀf[^ǂݍށB
     * 
     * @exception IOException o̓G[ꍇ
     */
    private void fillCache() throws IOException {
        this.markPositionIsInCache = false;
        this.cacheLimit            = 0;
        this.cachePosition         = 0;

        //cache Ƀf[^ǂݍ
        int read = 0;
        while( 0 <= read && this.cacheLimit < this.cache.length ){
            read = this.in.read( this.cache,
                                 this.cacheLimit, 
                                 this.cache.length - this.cacheLimit );         //throws IOException

            if( 0 < read ) this.cacheLimit += read;
        }
    }


    //------------------------------------------------------------------
    //  inner classes
    //------------------------------------------------------------------
    //  private static class LocalEOFException
    //------------------------------------------------------------------
    /**
     * BitInputStream  EndOfStream ̌o
     * EOFException gp̂͏X肪̂
     * [J EOFException `B
     */
    private static class LocalEOFException extends EOFException {


        //------------------------------------------------------------------
        //  instance field
        //------------------------------------------------------------------
        //  private Object owner
        //------------------------------------------------------------------
        /**
         * ̗O𓊂IuWFNg
         */
        private Object owner;

        //------------------------------------------------------------------
        //  constructer
        //------------------------------------------------------------------
        //  public LocalEOFException( Object object )
        //------------------------------------------------------------------
        /**
         * RXgN^B
         * 
         * @param object ̗O𓊂IuWFNg
         */
        public LocalEOFException( Object object ){
            super();
            this.owner = object;
        }

        //------------------------------------------------------------------
        //  access method
        //------------------------------------------------------------------
        //  public boolean thrownBy( Object object )
        //------------------------------------------------------------------
        /**
         * ̗O object ɂēꂽǂ𓾂B<br>
         * 
         * @param object IuWFNg
         * 
         * @return ̗O objectɂ
         *         ꂽOł true<br>
         *         Ⴆ false<br>
         */
        public boolean thrownBy( Object object ){
            return this.owner == object;
        }
    }

}
//end of BitInputStream.java