diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index 2a4edf57f..4a9a8f104 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -50,6 +50,11 @@ gson 2.2.4 + + com.jcraft + jzlib + 1.1.3 + @@ -119,6 +124,7 @@ java.util.logging java.util.concurrent java.util.regex + java.util.zip -output ${project.build.directory}/jcl-report diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDataFormatException.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDataFormatException.java new file mode 100644 index 000000000..c1ee24c95 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDataFormatException.java @@ -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 + */ +public class TDataFormatException extends Exception { + private static final long serialVersionUID = 7856637411580418624L; + + public TDataFormatException() { + super(); + } + + public TDataFormatException(String message) { + super(message); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDeflaterOutputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDeflaterOutputStream.java new file mode 100644 index 000000000..b1f1e85b6 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TDeflaterOutputStream.java @@ -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 + */ +public class TDeflaterOutputStream extends FilterOutputStream { + public TDeflaterOutputStream(OutputStream out) throws IOException { + super(out); + this.out = new DeflaterOutputStream(out); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPInputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPInputStream.java new file mode 100644 index 000000000..603e3262a --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPInputStream.java @@ -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 + */ +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); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPOutputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPOutputStream.java new file mode 100644 index 000000000..42d3a311c --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TGZIPOutputStream.java @@ -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 + */ +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); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TInflaterInputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TInflaterInputStream.java new file mode 100644 index 000000000..02287260a --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/zip/TInflaterInputStream.java @@ -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 + */ +public class TInflaterInputStream extends FilterInputStream { + public TInflaterInputStream(InputStream in) throws IOException { + super(in); + this.in = new InflaterInputStream(in); + } +} diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/zip/GZIPInputStreamTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/zip/GZIPInputStreamTest.java new file mode 100644 index 000000000..c265f59dc --- /dev/null +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/zip/GZIPInputStreamTest.java @@ -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 + */ +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)); + } +}