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