Add java.util.zip

This commit is contained in:
konsoletyper 2014-11-04 22:04:46 +03:00
parent d5e602b38e
commit 19c18a41c5
7 changed files with 233 additions and 0 deletions

View File

@ -50,6 +50,11 @@
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
@ -119,6 +124,7 @@
<argument>java.util.logging</argument>
<argument>java.util.concurrent</argument>
<argument>java.util.regex</argument>
<argument>java.util.zip</argument>
<argument>-output</argument>
<argument>${project.build.directory}/jcl-report</argument>
</arguments>

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TDataFormatException extends Exception {
private static final long serialVersionUID = 7856637411580418624L;
public TDataFormatException() {
super();
}
public TDataFormatException(String message) {
super(message);
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
import com.jcraft.jzlib.DeflaterOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TDeflaterOutputStream extends FilterOutputStream {
public TDeflaterOutputStream(OutputStream out) throws IOException {
super(out);
this.out = new DeflaterOutputStream(out);
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
import com.jcraft.jzlib.GZIPInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TGZIPInputStream extends TInflaterInputStream {
public TGZIPInputStream(InputStream in, int size) throws IOException {
super(in);
this.in = new GZIPInputStream(in, size, false);
}
public TGZIPInputStream(InputStream in) throws IOException {
super(in);
this.in = new GZIPInputStream(in);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
import com.jcraft.jzlib.GZIPOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TGZIPOutputStream extends TDeflaterOutputStream {
public TGZIPOutputStream(OutputStream out, int size, boolean syncFlush) throws IOException {
super(out);
this.out = new GZIPOutputStream(out, size, syncFlush);
}
public TGZIPOutputStream(OutputStream out, boolean syncFlush) throws IOException {
this(out, 512, syncFlush);
}
public TGZIPOutputStream(OutputStream out, int size) throws IOException {
this(out, size, true);
}
public TGZIPOutputStream(OutputStream out) throws IOException {
this(out, 512, true);
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
import com.jcraft.jzlib.InflaterInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TInflaterInputStream extends FilterInputStream {
public TInflaterInputStream(InputStream in) throws IOException {
super(in);
this.in = new InflaterInputStream(in);
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.zip;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import org.junit.Test;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class GZIPInputStreamTest {
@Test
public void gzipInputWorks() throws IOException {
String hex = "1f8b08086c1d59540003746561766d2d7a6970000b494d0cf355708ff20c5028cf2fca2e5648cbcc" +
"4b05005727a59115000000";
byte[] data = new byte[hex.length() / 2];
for (int i = 0; i < data.length; ++i) {
int h = Character.digit(hex.charAt(i * 2), 16);
int l = Character.digit(hex.charAt(i * 2 + 1), 16);
data[i] = (byte)((h << 4) | l);
}
GZIPInputStream input = new GZIPInputStream(new ByteArrayInputStream(data));
byte[] uncompressed = new byte[500];
int offset = 0;
while (true) {
int read = input.read(uncompressed, offset, uncompressed.length - offset);
if (read <= 0) {
break;
}
offset += read;
}
assertEquals(21, offset);
assertEquals("TeaVM GZIP works fine", new String(uncompressed, 0, 21));
}
}