File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\compress\LZFInputStream.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.compress;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11 import org.h2.message.DbException;
12 import org.h2.util.Utils;
13
14 /**
15 * An input stream to read from an LZF stream.
16 * The data is automatically expanded.
17 */
18 public class LZFInputStream extends InputStream {
19
20 private final InputStream in;
21 private CompressLZF decompress = new CompressLZF();
22 private int pos;
23 private int bufferLength;
24 private byte[] inBuffer;
25 private byte[] buffer;
26
27 public LZFInputStream(InputStream in) throws IOException {
28 this.in = in;
29 if (readInt() != LZFOutputStream.MAGIC) {
30 throw new IOException("Not an LZFInputStream");
31 }
32 }
33
34 private static byte[] ensureSize(byte[] buff, int len) {
35 return buff == null || buff.length < len ? Utils.newBytes(len) : buff;
36 }
37
38 private void fillBuffer() throws IOException {
39 if (buffer != null && pos < bufferLength) {
40 return;
41 }
42 int len = readInt();
43 if (decompress == null) {
44 // EOF
45 this.bufferLength = 0;
46 } else if (len < 0) {
47 len = -len;
48 buffer = ensureSize(buffer, len);
49 readFully(buffer, len);
50 this.bufferLength = len;
51 } else {
52 inBuffer = ensureSize(inBuffer, len);
53 int size = readInt();
54 readFully(inBuffer, len);
55 buffer = ensureSize(buffer, size);
56 try {
57 decompress.expand(inBuffer, 0, len, buffer, 0, size);
58 } catch (ArrayIndexOutOfBoundsException e) {
59 DbException.convertToIOException(e);
60 }
61 this.bufferLength = size;
62 }
63 pos = 0;
64 }
65
66 private void readFully(byte[] buff, int len) throws IOException {
67 int off = 0;
68 while (len > 0) {
69 int l = in.read(buff, off, len);
70 len -= l;
71 off += l;
72 }
73 }
74
75 private int readInt() throws IOException {
76 int x = in.read();
77 if (x < 0) {
78 decompress = null;
79 return 0;
80 }
81 x = (x << 24) + (in.read() << 16) + (in.read() << 8) + in.read();
82 return x;
83 }
84
85 public int read() throws IOException {
86 fillBuffer();
87 if (pos >= bufferLength) {
88 return -1;
89 }
90 return buffer[pos++] & 255;
91 }
92
93 public int read(byte[] b) throws IOException {
94 return read(b, 0, b.length);
95 }
96
97 public int read(byte[] b, int off, int len) throws IOException {
98 if (len == 0) {
99 return 0;
100 }
101 int read = 0;
102 while (len > 0) {
103 int r = readBlock(b, off, len);
104 if (r < 0) {
105 break;
106 }
107 read += r;
108 off += r;
109 len -= r;
110 }
111 return read == 0 ? -1 : read;
112 }
113
114 private int readBlock(byte[] b, int off, int len) throws IOException {
115 fillBuffer();
116 if (pos >= bufferLength) {
117 return -1;
118 }
119 int max = Math.min(len, bufferLength - pos);
120 max = Math.min(max, b.length - off);
121 System.arraycopy(buffer, pos, b, off, max);
122 pos += max;
123 return max;
124 }
125
126 public void close() throws IOException {
127 in.close();
128 }
129
130 }
131