]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.compressions/src/org/simantics/compressions/Compressions.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.compressions / src / org / simantics / compressions / Compressions.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.compressions;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileNotFoundException;\r
16 import java.io.InputStream;\r
17 import java.io.OutputStream;\r
18 import java.util.Collection;\r
19 import java.util.HashMap;\r
20 import java.util.Map;\r
21 \r
22 import org.osgi.framework.InvalidSyntaxException;\r
23 import org.osgi.framework.ServiceReference;\r
24 \r
25 /**\r
26  * A facade for accessing {@link CompressionCodec} implementations.\r
27  * \r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public final class Compressions {\r
31 \r
32         private static final Map<String, CompressionCodec> CODECS = new HashMap<>();\r
33     private static boolean initialized = false;\r
34 \r
35     public static final String LZ4 = "LZ4";\r
36     public static final String FASTLZ = "FASTLZ";\r
37     \r
38     private static void collectCodecs() {\r
39         try {\r
40             Collection<ServiceReference<CompressionCodec>> serv = Activator.getContext().getServiceReferences(CompressionCodec.class, null);\r
41             for (ServiceReference<CompressionCodec> service : serv) {\r
42                 CompressionCodec codec = Activator.getContext().getService(service);\r
43                 CODECS.put(codec.getId(), codec);\r
44             }\r
45             initialized = true;\r
46         } catch (InvalidSyntaxException e) {\r
47             e.printStackTrace();\r
48         }\r
49     }\r
50     \r
51         \r
52         /**\r
53          * Get a {@link CompressionCodec} matching the requested name.\r
54          * @param <T>\r
55          * \r
56          * @param codec codec name\r
57          * @return \r
58          * @return matching codec\r
59          * @see #FASTLZ\r
60          * @see #LZ4\r
61          */\r
62         public static CompressionCodec get(String codecId) {\r
63             if (!initialized) {\r
64                 collectCodecs();\r
65             }\r
66             CompressionCodec codec = CODECS.get(codecId);\r
67             if (codec == null) {\r
68                 throw new IllegalArgumentException("No codec with id " + codecId + " installed");\r
69             }\r
70             return codec;\r
71         }\r
72         \r
73         /**\r
74          * @param codecId\r
75          * @param file\r
76          * @return\r
77          * @throws FileNotFoundException \r
78          */\r
79         public static OutputStream write(String codecId, File file) throws FileNotFoundException {\r
80                 return get(codecId).write(file);\r
81         }\r
82 \r
83         /**\r
84          * @param codecId\r
85          * @param file\r
86          * @return\r
87          * @throws FileNotFoundException \r
88          */\r
89         public static InputStream read(String codecId, File file) throws FileNotFoundException {\r
90                 return get(codecId).read(file);\r
91         }\r
92 }