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