KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > io > RLE4InputStream


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package com.imagero.uio.io;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * PackBits decoder
38  *
39  * @author Andrei Kouznetsov
40  */

41 public class RLE4InputStream extends RLEInputStream {
42
43     byte[] value = new byte[2];
44
45     BitInputStream bin;
46
47     byte[] buffer = new byte[128];
48     int bufferStart;
49     int bufferLength;
50
51     ByteArrayOutputStreamExt bout;
52     BitOutputStream bitOut;
53
54     public RLE4InputStream(BitInputStream in) {
55         super(in);
56         bin = in;
57         bin.setBitsToRead(4);
58         bout = new ByteArrayOutputStreamExt();
59         bitOut = new BitOutputStream(bout);
60     }
61
62     public int read() throws IOException JavaDoc {
63         if (bufferStart >= bufferLength) {
64             fillBuffer();
65         }
66         if (bufferStart >= bufferLength) {
67             return -1;
68         }
69         return buffer[bufferStart++];
70     }
71
72     private void fillBuffer() throws IOException JavaDoc {
73         if (bout.size() == 0) {
74             fillBufferImpl();
75         }
76         bufferStart = 0;
77         bufferLength = bout.drain(buffer);
78     }
79
80     private void fillBufferImpl() throws IOException JavaDoc {
81         int len = bin.read(8);
82         if (len == 0) {
83             int value = bin.read(8);
84             switch (value) {
85                 case 0:
86                     throw new EndOfLineException();
87                 case 1:
88                     finished = true;
89                     throw new EndOfBitmapException();
90                 case 2:
91                     int x = bin.read(8);
92                     int y = bin.read(8);
93                     throw new DeltaRecordException(Integer.toHexString(x) + Integer.toHexString(y));
94                 default:
95                     int skipCount = 0;
96                     if ((value & 3) != 0) {
97                         skipCount = (value + 3) / 4 * 4 - value;
98                     }
99                     for (int i = 0; i < value; i++) {
100                         bitOut.write(bin.read(4), 4);
101                     }
102                     for (int i = 0; i < skipCount; i++) {
103                         /*int ignored = */bin.read(4);
104                     }
105             }
106         }
107         else {
108             value[0] = (byte) bin.read(4);
109             value[1] = (byte) bin.read(4);
110
111             for (int i = 0; i < len; i++) {
112                 bitOut.write(value[i & 1], 4);
113             }
114         }
115     }
116 }
117
Popular Tags