1 32 package com.imagero.uio.io; 33 34 import java.io.IOException ; 35 36 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 { 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 { 73 if (bout.size() == 0) { 74 fillBufferImpl(); 75 } 76 bufferStart = 0; 77 bufferLength = bout.drain(buffer); 78 } 79 80 private void fillBufferImpl() throws IOException { 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 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 |