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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.compressions;
\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
22 import org.osgi.framework.InvalidSyntaxException;
\r
23 import org.osgi.framework.ServiceReference;
\r
26 * A facade for accessing {@link CompressionCodec} implementations.
\r
28 * @author Tuukka Lehtonen
\r
30 public final class Compressions {
\r
32 private static final Map<String, CompressionCodec> CODECS = new HashMap<>();
\r
33 private static boolean initialized = false;
\r
35 public static final String LZ4 = "LZ4";
\r
36 public static final String FASTLZ = "FASTLZ";
\r
38 private static void collectCodecs() {
\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
46 } catch (InvalidSyntaxException e) {
\r
47 e.printStackTrace();
\r
53 * Get a {@link CompressionCodec} matching the requested name.
\r
56 * @param codec codec name
\r
58 * @return matching codec
\r
62 public static CompressionCodec get(String codecId) {
\r
66 CompressionCodec codec = CODECS.get(codecId);
\r
67 if (codec == null) {
\r
68 throw new IllegalArgumentException("No codec with id " + codecId + " installed");
\r
77 * @throws FileNotFoundException
\r
79 public static OutputStream write(String codecId, File file) throws FileNotFoundException {
\r
80 return get(codecId).write(file);
\r
87 * @throws FileNotFoundException
\r
89 public static InputStream read(String codecId, File file) throws FileNotFoundException {
\r
90 return get(codecId).read(file);
\r