--- /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;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.osgi.framework.InvalidSyntaxException;\r
+import org.osgi.framework.ServiceReference;\r
+\r
+/**\r
+ * A facade for accessing {@link CompressionCodec} implementations.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class Compressions {\r
+\r
+ private static final Map<String, CompressionCodec> CODECS = new HashMap<>();\r
+ private static boolean initialized = false;\r
+\r
+ public static final String LZ4 = "LZ4";\r
+ public static final String FASTLZ = "FASTLZ";\r
+ \r
+ private static void collectCodecs() {\r
+ try {\r
+ Collection<ServiceReference<CompressionCodec>> serv = Activator.getContext().getServiceReferences(CompressionCodec.class, null);\r
+ for (ServiceReference<CompressionCodec> service : serv) {\r
+ CompressionCodec codec = Activator.getContext().getService(service);\r
+ CODECS.put(codec.getId(), codec);\r
+ }\r
+ initialized = true;\r
+ } catch (InvalidSyntaxException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Get a {@link CompressionCodec} matching the requested name.\r
+ * @param <T>\r
+ * \r
+ * @param codec codec name\r
+ * @return \r
+ * @return matching codec\r
+ * @see #FASTLZ\r
+ * @see #LZ4\r
+ */\r
+ public static CompressionCodec get(String codecId) {\r
+ if (!initialized) {\r
+ collectCodecs();\r
+ }\r
+ CompressionCodec codec = CODECS.get(codecId);\r
+ if (codec == null) {\r
+ throw new IllegalArgumentException("No codec with id " + codecId + " installed");\r
+ }\r
+ return codec;\r
+ }\r
+ \r
+ /**\r
+ * @param codecId\r
+ * @param file\r
+ * @return\r
+ * @throws FileNotFoundException \r
+ */\r
+ public static OutputStream write(String codecId, File file) throws FileNotFoundException {\r
+ return get(codecId).write(file);\r
+ }\r
+\r
+ /**\r
+ * @param codecId\r
+ * @param file\r
+ * @return\r
+ * @throws FileNotFoundException \r
+ */\r
+ public static InputStream read(String codecId, File file) throws FileNotFoundException {\r
+ return get(codecId).read(file);\r
+ }\r
+}
\ No newline at end of file