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