diff src/main/java/com/glavsoft/rfb/encoding/decoder/DecodersContainer.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children e7ce2b2ffed8 17b702648079
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/DecodersContainer.java	Tue Jul 03 13:20:49 2012 +0900
@@ -0,0 +1,93 @@
+// Copyright (C) 2010, 2011 GlavSoft LLC.
+// All rights reserved.
+//
+//-------------------------------------------------------------------------
+// This file is part of the TightVNC software.  Please visit our Web site:
+//
+//                       http://www.tightvnc.com/
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//-------------------------------------------------------------------------
+//
+
+
+package com.glavsoft.rfb.encoding.decoder;
+
+import com.glavsoft.rfb.encoding.EncodingType;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+/**
+ * Decoders container class
+ */
+public class DecodersContainer {
+	private static Map<EncodingType, Class<? extends Decoder>> knownDecoders =
+		new HashMap<EncodingType, Class<? extends Decoder>>();
+	static {
+		knownDecoders.put(EncodingType.TIGHT, TightDecoder.class);
+		knownDecoders.put(EncodingType.HEXTILE, HextileDecoder.class);
+		knownDecoders.put(EncodingType.ZRLE, ZRLEDecoder.class);
+		knownDecoders.put(EncodingType.ZLIB, ZlibDecoder.class);
+		knownDecoders.put(EncodingType.RRE, RREDecoder.class);
+		knownDecoders.put(EncodingType.COPY_RECT, CopyRectDecoder.class);
+//		knownDecoders.put(EncodingType.RAW_ENCODING, RawDecoder.class);
+	}
+	private final Map<EncodingType, Decoder> decoders =
+		new HashMap<EncodingType, Decoder>();
+
+	public DecodersContainer() {
+		decoders.put(EncodingType.RAW_ENCODING, RawDecoder.getInstance());
+	}
+
+	/**
+	 * Instantiate decoders for encodings we are going to use.
+	 *
+	 * @param encodings encodings we need to handle
+	 */
+	public void instantiateDecodersWhenNeeded(Collection<EncodingType> encodings) {
+		for (EncodingType enc : encodings) {
+			if (EncodingType.ordinaryEncodings.contains(enc) && ! decoders.containsKey(enc)) {
+				try {
+					decoders.put(enc, knownDecoders.get(enc).newInstance());
+				} catch (InstantiationException e) {
+					logError(enc, e);
+				} catch (IllegalAccessException e) {
+					logError(enc, e);
+				}
+			}
+		}
+	}
+
+	private void logError(EncodingType enc, Exception e) {
+		Logger.getLogger(this.getClass().getName()).severe("Can not instantiate decoder for encoding type '" +
+				enc.getName() + "' " + e.getMessage());
+	}
+
+	public Decoder getDecoderByType(EncodingType type) {
+		return decoders.get(type);
+	}
+
+	public void resetDecoders() {
+		for (Decoder decoder : decoders.values()) {
+			if (decoder != null) {
+				decoder.reset();
+			}
+		}
+	}
+
+}