/
LzmaOutputStream.java
80 lines (67 loc) · 2.24 KB
/
LzmaOutputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// LzmaOutputStream.java -- transparently compress LZMA while writing
// Copyright (c)2007 Christopher League <league@contrapunctus.net>
// This is free software, but it comes with ABSOLUTELY NO WARRANTY.
// GNU Lesser General Public License 2.1 or Common Public License 1.0
package net.contrapunctus.lzma;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.PrintStream;
import SevenZip.Compression.LZMA.Encoder;
import java.io.ByteArrayInputStream;
public class LzmaOutputStream extends FilterOutputStream
{
protected EncoderThread eth;
public static boolean LZMA_HEADER = true;
/* true for compatibility with lzma(1) command-line tool, false
* for compatibility with previous versions of LZMA streams.
*/
private static final PrintStream dbg = System.err;
private static final boolean DEBUG;
static {
String ds = null;
try { ds = System.getProperty("DEBUG_LzmaStreams"); }
catch(SecurityException e) { }
DEBUG = ds != null;
}
public LzmaOutputStream( OutputStream _out )
{
this(_out, EncoderThread.DEFAULT_DICT_SZ_POW2, null);
}
public LzmaOutputStream( OutputStream _out, Integer dictSzPow2, Integer fastBytes )
{
super( null );
eth = new EncoderThread( _out, dictSzPow2, fastBytes );
out = ConcurrentBufferOutputStream.create( eth.q );
if(DEBUG) dbg.printf("%s >> %s (%s)%n", this, out, eth.q);
eth.start( );
}
public void write( int i ) throws IOException
{
if( eth.exn != null ) {
throw eth.exn;
}
out.write( i );
}
public void close( ) throws IOException
{
if(DEBUG) dbg.printf("%s closed%n", this);
out.close( );
try {
eth.join( );
if(DEBUG) dbg.printf("%s joined %s%n", this, eth);
}
catch( InterruptedException exn ) {
throw new InterruptedIOException( exn.getMessage() );
}
if( eth.exn != null ) {
throw eth.exn;
}
}
public String toString( )
{
return String.format("lzmaOut@%x", hashCode());
}
}