--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.compressions.impl;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.nio.ByteBuffer;\r
+import java.nio.ByteOrder;\r
+import java.nio.IntBuffer;\r
+import java.nio.channels.WritableByteChannel;\r
+\r
+/**\r
+ * @author Hannu Niemistö\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class CompressingOutputStream extends OutputStream {\r
+\r
+ protected static final int BLOCK_SIZE = 1024 * 1024;\r
+\r
+ protected OutputStream stream;\r
+ protected WritableByteChannel channel;\r
+ protected ByteBuffer compressed;\r
+ protected byte[] compressedArray;\r
+ protected ByteBuffer uncompressed;\r
+ protected IntBuffer header;\r
+ protected int bytes = 0;\r
+\r
+ public CompressingOutputStream(File file) throws FileNotFoundException {\r
+ this(new FileOutputStream(file));\r
+ }\r
+\r
+ public CompressingOutputStream(FileOutputStream stream) {\r
+ this(stream, stream.getChannel());\r
+ }\r
+\r
+ public CompressingOutputStream(OutputStream stream) {\r
+ this(stream, null);\r
+ }\r
+\r
+ public CompressingOutputStream(OutputStream stream, WritableByteChannel channel) {\r
+ this.stream = stream;\r
+ this.channel = channel;\r
+ uncompressed = allocateBuffer(BLOCK_SIZE);\r
+ }\r
+\r
+ @Override\r
+ public void write(int b) throws IOException {\r
+ if(uncompressed.remaining() == 0)\r
+ flushBuffer();\r
+ uncompressed.put((byte)b);\r
+ ++bytes;\r
+ }\r
+\r
+ @Override\r
+ public void write(byte[] b, int off, int len) throws IOException {\r
+ bytes += len;\r
+ while(len > 0) {\r
+ int s = Math.min(len, uncompressed.remaining());\r
+ uncompressed.put(b, off, s);\r
+ len -= s;\r
+ off += s;\r
+ if(uncompressed.remaining() == 0)\r
+ flushBuffer();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void close() throws IOException {\r
+ try {\r
+ flushBuffer();\r
+\r
+ if (channel != null) {\r
+ header.position(0);\r
+ header.put(0);\r
+ header.put(0);\r
+ compressed.position(0);\r
+ compressed.limit(8);\r
+ channel.write(compressed);\r
+ } else if (stream != null) {\r
+ stream.write(new byte[8]);\r
+ }\r
+ //System.out.println("Wrote " + bytes + " uncompressed bytes.");\r
+ } finally {\r
+ if (channel != null) {\r
+ channel.close();\r
+ channel = null;\r
+ }\r
+ if (stream != null) {\r
+ stream.close();\r
+ stream = null;\r
+ }\r
+ }\r
+ }\r
+\r
+ private void flushBuffer() throws IOException {\r
+ int uncompressedSize = uncompressed.position();\r
+ uncompressed.position(0);\r
+ int maxCompressedSize = compressBound(uncompressedSize) + 8;\r
+ if(compressed == null || compressed.capacity() < maxCompressedSize) {\r
+ compressed = allocateBuffer(maxCompressedSize);\r
+ compressed.order(ByteOrder.LITTLE_ENDIAN);\r
+ header = compressed.asIntBuffer();\r
+ if (channel == null)\r
+ compressedArray = new byte[maxCompressedSize];\r
+ }\r
+ int compressedSize = compress(uncompressed, 0, uncompressedSize, \r
+ compressed, 8);\r
+ header.position(0);\r
+ header.put(compressedSize);\r
+ header.put(uncompressedSize);\r
+ compressed.position(0);\r
+ compressed.limit(compressedSize+8);\r
+ if (channel != null) {\r
+ channel.write(compressed);\r
+ } else {\r
+ compressed.get(compressedArray, 0, compressedSize+8);\r
+ stream.write(compressedArray, 0, compressedSize+8);\r
+ }\r
+ }\r
+\r
+ protected abstract int compressBound(int inputSize);\r
+\r
+ protected abstract int compress(ByteBuffer uncompressed, int uncompressedOffset, int uncompressedSize,\r
+ ByteBuffer compressed, int compressedOffset) throws IOException;\r
+\r
+ protected abstract ByteBuffer allocateBuffer(int capacity);\r
+\r
+}
\ No newline at end of file