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