1 30 31 package org.ofbiz.service.rmi.socket.zip; 32 33 import java.io.EOFException ; 34 import java.io.FilterInputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 38 public class CompressionInputStream extends FilterInputStream implements CompressionConstants { 39 42 public CompressionInputStream(InputStream in) { 43 super(in); 44 } 45 46 50 int buf[] = new int[5]; 51 52 55 int bufPos = 5; 56 57 60 61 public int read() throws IOException { 62 try { 63 int code; 64 65 do { 68 code = readCode(); 69 } while (code == NOP); 70 71 if (code >= BASE) { 72 return codeTable.charAt(code - BASE); 75 } else if (code == RAW) { 76 int high = readCode(); 79 int low = readCode(); 80 return (high << 4) | low; 81 } else 82 throw new IOException ("unknown compression code: " + code); 83 } catch (EOFException e) { 84 return -1; 86 } 87 } 88 89 93 public int read(byte b[], int off, int len) throws IOException { 94 95 if (len <= 0) { 96 return 0; 97 } 98 99 int c = read(); 100 if (c == -1) { 101 return -1; 102 } 103 b[off] = (byte) c; 104 105 int i = 1; 106 try { 109 for (; (i < len) && (in.available() > 0); i++) { 110 c = read(); 111 if (c == -1) { 112 break; 113 } 114 if (b != null) { 115 b[off + i] = (byte) c; 116 } 117 } 118 } catch (IOException ee) { 119 } 120 return i; 121 } 122 123 128 private int readCode() throws IOException { 129 if (bufPos == 5) { 132 int b1 = in.read(); 133 int b2 = in.read(); 134 int b3 = in.read(); 135 int b4 = in.read(); 136 137 if ((b1 | b2 | b3 | b4) < 0) { 140 throw new EOFException (); 141 } 142 int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; 145 buf[0] = (pack >>> 24) & 0x3F; 146 buf[1] = (pack >>> 18) & 0x3F; 147 buf[2] = (pack >>> 12) & 0x3F; 148 buf[3] = (pack >>> 6) & 0x3F; 149 buf[4] = (pack >>> 0) & 0x3F; 150 bufPos = 0; 151 } 152 return buf[bufPos++]; 153 } 154 } 155 | Popular Tags |