001 /*
002 * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003 *
004 * http://uio.imagero.com
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 * o Redistributions of source code must retain the above copyright notice,
010 * this list of conditions and the following disclaimer.
011 *
012 * o Redistributions in binary form must reproduce the above copyright notice,
013 * this list of conditions and the following disclaimer in the documentation
014 * and/or other materials provided with the distribution.
015 *
016 * o Neither the name of Andrey Kuznetsov nor the names of
017 * its contributors may be used to endorse or promote products derived
018 * from this software without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032
033 package com.imagero.uio.io;
034
035 import java.io.FilterInputStream;
036 import java.io.IOException;
037 import java.io.InputStream;
038
039 /**
040 * PackBits decoder
041 *
042 * @author Andrei Kouznetsov
043 */
044 public class PackBitsInputStream extends FilterInputStream {
045
046 boolean finished;
047
048 int numSamples, value;
049 boolean copyLiter;
050
051 public PackBitsInputStream(InputStream in) {
052 super(in);
053 }
054
055 public int available() throws IOException {
056 if(finished) {
057 return 0;
058 }
059 return 1;
060 }
061
062 public void close() throws IOException {
063 }
064
065 public boolean markSupported() {
066 return false;
067 }
068
069 public synchronized void mark(int readlimit) {
070 }
071
072 public synchronized void reset() throws IOException {
073 throw new IOException("mark/reset not supported");
074 }
075
076 public int read(byte b[]) throws IOException {
077 return read(b, 0, b.length);
078 }
079
080 public int read(byte b[], int off, int len) throws IOException {
081 int i = off;
082
083 try {
084 for(; i < off + len; i++) {
085 int a = read();
086 if(a == -1) {
087 i--;
088 break;
089 }
090 b[i] = (byte) a;
091 }
092 }
093 catch(IOException ex) {
094 ex.printStackTrace();
095 }
096 return i - off;
097 }
098
099 public int read() throws IOException {
100 if(numSamples == 0) {
101 byte l = read128();
102 if(l < 0) {
103 numSamples = -l + 1;
104 copyLiter = false;
105 value = in.read();
106 }
107 else {
108 numSamples = l + 1;
109 copyLiter = true;
110 }
111 }
112 numSamples--;
113 if(copyLiter) {
114 return in.read();
115 }
116 else {
117 return value;
118 }
119 }
120
121 byte read128() throws IOException {
122 do {
123 byte a = (byte) in.read();
124 if(a != -128) {
125 return a;
126 }
127 }
128 while(true);
129 }
130 }