1 32 package com.imagero.uio.io; 33 34 import java.io.FilterInputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 38 43 public class PackBitsInputStream extends FilterInputStream { 44 45 boolean finished; 46 47 int numSamples, value; 48 boolean copyLiter; 49 50 public PackBitsInputStream(InputStream in) { 51 super(in); 52 } 53 54 public int available() throws IOException { 55 if(finished) { 56 return 0; 57 } 58 return 1; 59 } 60 61 public void close() throws IOException { 62 } 63 64 public boolean markSupported() { 65 return false; 66 } 67 68 public synchronized void mark(int readlimit) { 69 } 70 71 public synchronized void reset() throws IOException { 72 throw new IOException ("mark/reset not supported"); 73 } 74 75 public int read(byte b[]) throws IOException { 76 return read(b, 0, b.length); 77 } 78 79 public int read(byte b[], int off, int len) throws IOException { 80 int i = off; 81 82 try { 83 for(; i < off + len; i++) { 84 int a = read(); 85 if(a == -1) { 86 i--; 87 break; 88 } 89 b[i] = (byte) a; 90 } 91 } 92 catch(IOException ex) { 93 ex.printStackTrace(); 94 } 95 return i - off; 96 } 97 98 public int read() throws IOException { 99 if(numSamples == 0) { 100 byte l = read128(); 101 if(l < 0) { 102 numSamples = -l + 1; 103 copyLiter = false; 104 value = in.read(); 105 } 106 else { 107 numSamples = l + 1; 108 copyLiter = true; 109 } 110 } 111 numSamples--; 112 if(copyLiter) { 113 return in.read(); 114 } 115 else { 116 return value; 117 } 118 } 119 120 byte read128() throws IOException { 121 do { 122 byte a = (byte) in.read(); 123 if(a != -128) { 124 return a; 125 } 126 } 127 while(true); 128 } 129 } 130 | Popular Tags |