/** -*- tab-width: 4 -*-
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2010 by Trifork
 *
 * 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 erjang.m.zlib;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

import erjang.BIF;
import erjang.EBinary;
import erjang.ENative;
import erjang.EObject;
import erjang.ERT;

public class Native extends ENative {

	@BIF
	public static EBinary compress(EObject bin) {
		
		EBinary b = bin.testBinary();
		if (b == null) {
			throw ERT.badarg(bin);
		}
		
		Deflater defl = new Deflater();
		BARR bos = new BARR();
		DeflaterOutputStream dos = new DeflaterOutputStream(bos, defl);
		
		try {
			b.writeTo(dos);
			dos.close();
		} catch (IOException e) {
			throw new InternalError("should not happen");
		}
		
		return bos.asBinary();		
	}
	
	static private class BARR extends ByteArrayOutputStream {
		EBinary asBinary() {
			return new EBinary(super.buf, 0, super.count);
		}
	}


}

@@ -880,16 +880,42 @@
 Binary;%0A
+import erjang.EBitString;%0A
 import e
@@ -929,16 +929,16 @@
 Native;%0A
-
 import e
@@ -970,16 +970,51 @@
 ang.ERT;
+%0Aimport erjang.m.erlang.ErlConvert;
 %0A%0Apublic
@@ -1151,25 +1151,151 @@
 = null) %7B%0A%09%09
-%09
+    EBitString bin2 = ErlConvert.iolist_to_binary(bin);%0A%09        b = bin2.testBinary();%0A%09        if (b == null) %7B%0A%09            
 throw ERT.ba
@@ -1305,16 +1305,27 @@
 g(bin);%0A
+%09        %7D%0A
 %09%09%7D%0A%09%09%0A%09

Allow zlib:compress/1 arg to be an iolist