--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.db.procore.cluster;\r
+\r
+import java.nio.ByteBuffer;\r
+import java.nio.ByteOrder;\r
+\r
+import org.simantics.db.exception.InternalException;\r
+\r
+public class FastLZ {\r
+ static private final int InLength = 1<<20;\r
+ \r
+ public static class DecompressStruct {\r
+ public long[] longs;\r
+ public int[] ints;\r
+ public byte[] bytes;\r
+ }\r
+ static class SourceData {\r
+ SourceData(byte[] data) {\r
+ this.data = data;\r
+ this.offset = 0;\r
+ }\r
+ int left() {\r
+ return data.length - offset;\r
+ }\r
+ int readInt() {\r
+ int inLength = 4; // sizeof(int)\r
+ assert(left() >= inLength); // must have data for input\r
+ ByteBuffer buffer = ByteBuffer.wrap(data, offset, inLength);\r
+ buffer.order(ByteOrder.LITTLE_ENDIAN); // argh!\r
+ int t = buffer.getInt();\r
+ offset += inLength;\r
+ return t;\r
+ }\r
+ void readBytes(byte[] bytes, int aOffset, int inLength) {\r
+ assert(left() >= inLength); // must have data for input\r
+ System.arraycopy(data, offset, bytes, aOffset, inLength);\r
+ offset += inLength;\r
+ }\r
+ byte[] data;\r
+ int offset;\r
+ }\r
+ public static DecompressStruct decompress(byte[] data) throws InternalException {\r
+ assert(data.length > 12); // 3*(table size)\r
+ SourceData sourceData = new SourceData(data);\r
+ DecompressStruct struct = new DecompressStruct();\r
+ int longSize = sourceData.readInt();\r
+ int intSize = sourceData.readInt();\r
+ int byteSize = sourceData.readInt();\r
+ struct.longs = new long[longSize]; \r
+ try {\r
+ decompress(sourceData, struct.longs);\r
+ struct.ints = new int[intSize]; \r
+ decompress(sourceData, struct.ints);\r
+ struct.bytes = new byte[byteSize];\r
+ decompress(sourceData, struct.bytes);\r
+ } catch (CompressionException e) {\r
+ throw new InternalException("Failed to decompress.", e);\r
+ }\r
+ return struct;\r
+ }\r
+ private static void decompress(SourceData sourceData, long[] longs) throws CompressionException {\r
+ int length = longs.length * 8;\r
+ ByteBuffer bytes = ByteBuffer.allocate(length);\r
+ int size = decompressRaw(sourceData, bytes.array());\r
+ assert(size == length);\r
+ bytes.order(ByteOrder.LITTLE_ENDIAN); // argh\r
+ for (int i=0; i<longs.length; ++i)\r
+ longs[i] = bytes.getLong();\r
+ }\r
+ private static void decompress(SourceData sourceData, int[] ints) throws CompressionException {\r
+ int length = ints.length * 4;\r
+ ByteBuffer bytes = ByteBuffer.allocate(length);\r
+ int size = decompressRaw(sourceData, bytes.array());\r
+ assert(size == length);\r
+ bytes.order(ByteOrder.LITTLE_ENDIAN); // argh\r
+ for (int i=0; i<ints.length; ++i)\r
+ ints[i] = bytes.getInt();\r
+ }\r
+ private static void decompress(SourceData sourceData, byte[] bytes) throws CompressionException {\r
+ int size = decompressRaw(sourceData, bytes);\r
+ assert(size == bytes.length);\r
+ }\r
+ private static int decompressRaw(SourceData sourceData, byte[] bytes) throws CompressionException {\r
+ int aDstSize = bytes.length;\r
+ if (aDstSize < 1)\r
+ return aDstSize;\r
+ int dstOffset = 0;\r
+ for (int dstSize = 0;;) {\r
+ int dataLength = sourceData.readInt(); \r
+ assert(dataLength <= InLength); // data is written in blocks\r
+ if (0 == dataLength) // EOF\r
+ return dstSize;\r
+ dstSize += dataLength;\r
+ int inLength = sourceData.readInt();\r
+\r
+ assert(aDstSize >= dstSize); // must have space for data\r
+ assert(sourceData.left() >= inLength); // must have data for input\r
+ assert(inLength > 0); // no data no block\r
+ assert(inLength <= InLength); // input size is block size or less\r
+ assert(inLength <= dataLength); // input size is never bigger than data size\r
+\r
+ if (inLength < dataLength) {// block was actually compressed\r
+ decompress(sourceData.data, sourceData.offset, inLength, bytes, dstOffset, dataLength);\r
+ sourceData.offset += inLength;\r
+ dstOffset += dataLength;\r
+ } else {\r
+ sourceData.readBytes(bytes, dstOffset, inLength);\r
+ dstOffset += inLength;\r
+ }\r
+ }\r
+ }\r
+ private static void decompress(byte[] in, int inOffset, int inLength, byte[] out, int outOffset, int outLength) throws CompressionException {\r
+ ByteBuffer inBuffer = ByteBuffer.allocateDirect(inLength);\r
+ inBuffer.put(in, inOffset, inLength);\r
+ inBuffer.flip();\r
+ ByteBuffer outBuffer = ByteBuffer.allocateDirect(outLength);\r
+ int inflateSize = org.simantics.fastlz.FastLZ.decompressBuffer(inBuffer, 0, inLength, outBuffer, 0, outLength);\r
+ if (inflateSize != outLength)\r
+ throw new RuntimeException("Decompression error.");\r
+ //outBuffer.flip();\r
+ outBuffer.get(out, outOffset, outLength);\r
+ }\r
+}\r