comparison src/com/tmate/hgkit/fs/InflaterDataAccess.java @ 51:9429c7bd1920 wrap-data-access

Try DataAccess to reach revision data instead of plain byte arrays
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 16 Jan 2011 01:20:26 +0100
parents
children
comparison
equal deleted inserted replaced
50:f1db8610da62 51:9429c7bd1920
1 /*
2 * Copyright (c) 2011 Artem Tikhomirov
3 */
4 package com.tmate.hgkit.fs;
5
6 import java.io.EOFException;
7 import java.io.IOException;
8 import java.util.zip.DataFormatException;
9 import java.util.zip.Inflater;
10 import java.util.zip.ZipException;
11
12 /**
13 * DataAccess counterpart for InflaterInputStream.
14 * XXX is it really needed to be subclass of FilterDataAccess?
15 * @author artem
16 */
17 public class InflaterDataAccess extends FilterDataAccess {
18
19 private final Inflater inflater;
20 private final byte[] buffer;
21 private final byte[] singleByte = new byte[1];
22 private int decompressedPos = 0;
23 private int decompressedLength = -1;
24
25 public InflaterDataAccess(DataAccess dataAccess, long offset, int length) {
26 this(dataAccess, offset, length, new Inflater(), 512);
27 }
28
29 public InflaterDataAccess(DataAccess dataAccess, long offset, int length, Inflater inflater, int bufSize) {
30 super(dataAccess, offset, length);
31 this.inflater = inflater;
32 buffer = new byte[bufSize];
33 }
34
35 @Override
36 public void reset() throws IOException {
37 super.reset();
38 inflater.reset();
39 decompressedPos = 0;
40 }
41
42 @Override
43 protected int available() {
44 throw new IllegalStateException("Can't tell how much uncompressed data left");
45 }
46
47 @Override
48 public boolean isEmpty() {
49 return super.available() <= 0 && inflater.finished(); // and/or inflater.getRemaining() <= 0 ?
50 }
51
52 @Override
53 public long length() {
54 if (decompressedLength != -1) {
55 return decompressedLength;
56 }
57 int c = 0;
58 try {
59 int oldPos = decompressedPos;
60 while (!isEmpty()) {
61 readByte();
62 c++;
63 }
64 decompressedLength = c + oldPos;
65 reset();
66 seek(oldPos);
67 return decompressedLength;
68 } catch (IOException ex) {
69 ex.printStackTrace(); // FIXME log error
70 decompressedLength = -1; // better luck next time?
71 return 0;
72 }
73 }
74
75 @Override
76 public void seek(long localOffset) throws IOException {
77 System.out.println("Seek: " + localOffset);
78 if (localOffset < 0 /* || localOffset >= length() */) {
79 throw new IllegalArgumentException();
80 }
81 if (localOffset >= decompressedPos) {
82 skip((int) (localOffset - decompressedPos));
83 } else {
84 reset();
85 skip((int) localOffset);
86 }
87 }
88
89 @Override
90 public void skip(int bytes) throws IOException {
91 if (bytes < 0) {
92 bytes += decompressedPos;
93 if (bytes < 0) {
94 throw new IOException("Underflow. Rewind past start of the slice.");
95 }
96 reset();
97 // fall-through
98 }
99 while (!isEmpty() && bytes > 0) {
100 readByte();
101 bytes--;
102 }
103 if (bytes != 0) {
104 throw new IOException("Underflow. Rewind past end of the slice");
105 }
106 }
107
108 @Override
109 public byte readByte() throws IOException {
110 readBytes(singleByte, 0, 1);
111 return singleByte[0];
112 }
113
114 @Override
115 public void readBytes(byte[] b, int off, int len) throws IOException {
116 try {
117 int n;
118 while (len > 0) {
119 while ((n = inflater.inflate(b, off, len)) == 0) {
120 if (inflater.finished() || inflater.needsDictionary()) {
121 throw new EOFException();
122 }
123 if (inflater.needsInput()) {
124 // fill:
125 int toRead = super.available();
126 if (toRead > buffer.length) {
127 toRead = buffer.length;
128 }
129 super.readBytes(buffer, 0, toRead);
130 inflater.setInput(buffer, 0, toRead);
131 }
132 }
133 off += n;
134 len -= n;
135 decompressedPos += n;
136 if (len == 0) {
137 return; // filled
138 }
139 }
140 } catch (DataFormatException e) {
141 String s = e.getMessage();
142 throw new ZipException(s != null ? s : "Invalid ZLIB data format");
143 }
144 }
145 }