01: // LZ.OutWindow
02:
03: package SevenZip.Compression.lz;
04:
05: import java.io.IOException;
06:
07: public class OutWindow {
08: byte[] _buffer;
09: int _pos;
10: int _windowSize = 0;
11: int _streamPos;
12: java.io.OutputStream _stream;
13:
14: public void Create(int windowSize) {
15: if (_buffer == null || _windowSize != windowSize)
16: _buffer = new byte[windowSize];
17: _windowSize = windowSize;
18: _pos = 0;
19: _streamPos = 0;
20: }
21:
22: public void SetStream(java.io.OutputStream stream)
23: throws IOException {
24: ReleaseStream();
25: _stream = stream;
26: }
27:
28: public void ReleaseStream() throws IOException {
29: Flush();
30: _stream = null;
31: }
32:
33: public void Init(boolean solid) {
34: if (!solid) {
35: _streamPos = 0;
36: _pos = 0;
37: }
38: }
39:
40: public void Flush() throws IOException {
41: int size = _pos - _streamPos;
42: if (size == 0)
43: return;
44: _stream.write(_buffer, _streamPos, size);
45: if (_pos >= _windowSize)
46: _pos = 0;
47: _streamPos = _pos;
48: }
49:
50: public void CopyBlock(int distance, int len) throws IOException {
51: int pos = _pos - distance - 1;
52: if (pos < 0)
53: pos += _windowSize;
54: for (; len != 0; len--) {
55: if (pos >= _windowSize)
56: pos = 0;
57: _buffer[_pos++] = _buffer[pos++];
58: if (_pos >= _windowSize)
59: Flush();
60: }
61: }
62:
63: public void PutByte(byte b) throws IOException {
64: _buffer[_pos++] = b;
65: if (_pos >= _windowSize)
66: Flush();
67: }
68:
69: public byte GetByte(int distance) {
70: int pos = _pos - distance - 1;
71: if (pos < 0)
72: pos += _windowSize;
73: return _buffer[pos];
74: }
75: }
|