19
19
package org .apache .sshd .common .compression ;
20
20
21
21
import java .io .IOException ;
22
+ import java .util .zip .DataFormatException ;
23
+ import java .util .zip .Deflater ;
24
+ import java .util .zip .Inflater ;
22
25
23
- import com .jcraft .jzlib .JZlib ;
24
- import com .jcraft .jzlib .ZStream ;
25
26
import org .apache .sshd .common .Compression ;
26
27
import org .apache .sshd .common .NamedFactory ;
27
- import org .apache .sshd .common .SshConstants ;
28
- import org .apache .sshd .common .SshException ;
29
28
import org .apache .sshd .common .util .Buffer ;
30
29
31
30
/**
@@ -50,8 +49,9 @@ public Compression create() {
50
49
51
50
static private final int BUF_SIZE = 4096 ;
52
51
53
- private ZStream stream ;
54
52
private byte [] tmpbuf = new byte [BUF_SIZE ];
53
+ private Deflater compresser ;
54
+ private Inflater decompresser ;
55
55
56
56
/**
57
57
* Create a new instance of a ZLib base compression
@@ -64,54 +64,28 @@ public boolean isDelayed() {
64
64
}
65
65
66
66
public void init (Type type , int level ) {
67
- stream = new ZStream ();
68
- if (type == Type .Deflater ) {
69
- stream .deflateInit (level );
70
- } else {
71
- stream .inflateInit ();
72
- }
67
+ compresser = new Deflater (level );
68
+ decompresser = new Inflater ();
73
69
}
74
70
75
71
public void compress (Buffer buffer ) throws IOException {
76
- stream .next_in = buffer .array ();
77
- stream .next_in_index = buffer .rpos ();
78
- stream .avail_in = buffer .available ();
72
+ compresser .setInput (buffer .array (), buffer .rpos (), buffer .available ());
79
73
buffer .wpos (buffer .rpos ());
80
- do {
81
- stream .next_out = tmpbuf ;
82
- stream .next_out_index = 0 ;
83
- stream .avail_out = BUF_SIZE ;
84
- int status = stream .deflate (JZlib .Z_PARTIAL_FLUSH );
85
- switch (status ) {
86
- case JZlib .Z_OK :
87
- buffer .putRawBytes (tmpbuf , 0 , BUF_SIZE - stream .avail_out );
88
- break ;
89
- default :
90
- throw new SshException (SshConstants .SSH2_DISCONNECT_COMPRESSION_ERROR , "compress: deflate returned " + status );
91
- }
74
+ int len ;
75
+ while ((len = compresser .deflate (tmpbuf , 0 , tmpbuf .length , Deflater .SYNC_FLUSH )) > 0 ) {
76
+ buffer .putRawBytes (tmpbuf , 0 , len );
92
77
}
93
- while (stream .avail_out == 0 );
94
78
}
95
79
96
80
public void uncompress (Buffer from , Buffer to ) throws IOException {
97
- stream .next_in = from .array ();
98
- stream .next_in_index = from .rpos ();
99
- stream .avail_in = from .available ();
100
-
101
- while (true ) {
102
- stream .next_out = tmpbuf ;
103
- stream .next_out_index = 0 ;
104
- stream .avail_out = BUF_SIZE ;
105
- int status = stream .inflate (JZlib .Z_PARTIAL_FLUSH );
106
- switch (status ) {
107
- case JZlib .Z_OK :
108
- to .putRawBytes (tmpbuf , 0 , BUF_SIZE - stream .avail_out );
109
- break ;
110
- case JZlib .Z_BUF_ERROR :
111
- return ;
112
- default :
113
- throw new SshException (SshConstants .SSH2_DISCONNECT_COMPRESSION_ERROR , "uncompress: inflate returned " + status );
81
+ decompresser .setInput (from .array (), from .rpos (), from .available ());
82
+ int len ;
83
+ try {
84
+ while ((len = decompresser .inflate (tmpbuf )) > 0 ) {
85
+ to .putRawBytes (tmpbuf , 0 , len );
114
86
}
87
+ } catch (DataFormatException e ) {
88
+ throw new IOException ("Error decompressing data" , e );
115
89
}
116
90
}
117
91
0 commit comments