⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bdecoder.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   BDecoder.java

package org.gudy.azureus2.core3.util;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.*;

// Referenced classes of package org.gudy.azureus2.core3.util:
//			BEncodingException, ByteFormatter, Constants, Debug, 
//			LightHashMap, StringInterner

public class BDecoder
{
	private static interface BDecoderInputStream
	{

		public abstract int read()
			throws IOException;

		public abstract int read(byte abyte0[])
			throws IOException;

		public abstract int read(byte abyte0[], int i, int j)
			throws IOException;

		public abstract int available()
			throws IOException;

		public abstract boolean markSupported();

		public abstract void mark(int i);

		public abstract void reset()
			throws IOException;
	}

	private class BDecoderInputStreamArray
		implements BDecoderInputStream
	{

		private final byte buffer[];
		private final int count;
		private int pos;
		private int mark;
		final BDecoder this$0;

		public int read()
			throws IOException
		{
			return pos >= count ? -1 : buffer[pos++] & 0xff;
		}

		public int read(byte buffer[])
			throws IOException
		{
			return read(buffer, 0, buffer.length);
		}

		public int read(byte b[], int offset, int length)
			throws IOException
		{
			if (pos >= count)
				return -1;
			if (pos + length > count)
				length = count - pos;
			if (length <= 0)
			{
				return 0;
			} else
			{
				System.arraycopy(buffer, pos, b, offset, length);
				pos += length;
				return length;
			}
		}

		public int available()
			throws IOException
		{
			return count - pos;
		}

		public boolean markSupported()
		{
			return true;
		}

		public void mark(int limit)
		{
			mark = pos;
		}

		public void reset()
			throws IOException
		{
			pos = mark;
		}

		private BDecoderInputStreamArray(byte _buffer[])
		{
			this$0 = BDecoder.this;
			super();
			buffer = _buffer;
			count = buffer.length;
		}

		private BDecoderInputStreamArray(byte _buffer[], int _offset, int _length)
		{
			this$0 = BDecoder.this;
			super();
			buffer = _buffer;
			pos = _offset;
			count = Math.min(_offset + _length, _buffer.length);
			mark = _offset;
		}


	}

	private class BDecoderInputStreamStream
		implements BDecoderInputStream
	{

		private final BufferedInputStream is;
		final BDecoder this$0;

		public int read()
			throws IOException
		{
			return is.read();
		}

		public int read(byte buffer[])
			throws IOException
		{
			return is.read(buffer);
		}

		public int read(byte buffer[], int offset, int length)
			throws IOException
		{
			return is.read(buffer, offset, length);
		}

		public int available()
			throws IOException
		{
			return is.available();
		}

		public boolean markSupported()
		{
			return is.markSupported();
		}

		public void mark(int limit)
		{
			is.mark(limit);
		}

		public void reset()
			throws IOException
		{
			is.reset();
		}

		private BDecoderInputStreamStream(BufferedInputStream _is)
		{
			this$0 = BDecoder.this;
			super();
			is = _is;
		}

	}


	private static final boolean TRACE = false;
	private boolean recovery_mode;
	private final char numberChars[] = new char[32];

	public static Map decode(byte data[])
		throws IOException
	{
		return (new BDecoder()).decodeByteArray(data);
	}

	public static Map decode(byte data[], int offset, int length)
		throws IOException
	{
		return (new BDecoder()).decodeByteArray(data, offset, length);
	}

	public static Map decode(BufferedInputStream is)
		throws IOException
	{
		return (new BDecoder()).decodeStream(is);
	}

	public BDecoder()
	{
	}

	public Map decodeByteArray(byte data[])
		throws IOException
	{
		return decode(new BDecoderInputStreamArray(data));
	}

	public Map decodeByteArray(byte data[], int offset, int length)
		throws IOException
	{
		return decode(new BDecoderInputStreamArray(data, offset, length));
	}

	public Map decodeStream(BufferedInputStream data)
		throws IOException
	{
		Object res = decodeInputStream(new BDecoderInputStreamStream(data), 0);
		if (res == null)
			throw new BEncodingException("BDecoder: zero length file");
		if (!(res instanceof Map))
			throw new BEncodingException("BDecoder: top level isn't a Map");
		else
			return (Map)res;
	}

	private Map decode(BDecoderInputStream data)
		throws IOException
	{
		Object res = decodeInputStream(data, 0);
		if (res == null)
			throw new BEncodingException("BDecoder: zero length file");
		if (!(res instanceof Map))
			throw new BEncodingException("BDecoder: top level isn't a Map");
		else
			return (Map)res;
	}

	private Object decodeInputStream(BDecoderInputStream dbis, int nesting)
		throws IOException
	{
		if (nesting == 0 && !dbis.markSupported())
			throw new IOException("InputStream must support the mark() method");
		dbis.mark(0x7fffffff);
		int tempByte = dbis.read();
		switch (tempByte)
		{
		case 100: // 'd'
			Map tempMap = new LightHashMap();
			try
			{
				byte tempByteArray[] = null;
				do
				{
					if ((tempByteArray = (byte[])(byte[])decodeInputStream(dbis, nesting + 1)) == null)
						break;
					Object value = decodeInputStream(dbis, nesting + 1);
					String key = null;
					if (key == null)
					{
						CharBuffer cb = Constants.BYTE_CHARSET.decode(ByteBuffer.wrap(tempByteArray));
						key = new String(cb.array(), 0, cb.limit());
						key = StringInterner.intern(key);
					}
					if (value == null)
					{
						Debug.out((new StringBuilder()).append("Invalid encoding - value not serialsied for '").append(key).append("' - ignoring").toString());
						break;
					}
					tempMap.put(key, value);
				} while (true);
				dbis.mark(0x7fffffff);
				tempByte = dbis.read();
				dbis.reset();
				if (nesting > 0 && tempByte == -1)
					throw new BEncodingException("BDecoder: invalid input data, 'e' missing from end of dictionary");
			}
			catch (Throwable e)
			{
				if (!recovery_mode)
					if (e instanceof IOException)
						throw (IOException)e;
					else
						throw new IOException(Debug.getNestedExceptionMessage(e));
			}
			if (tempMap instanceof LightHashMap)
				((LightHashMap)tempMap).compactify(-0.9F);
			return tempMap;

		case 108: // 'l'
			ArrayList tempList = new ArrayList();
			try
			{
				for (Object tempElement = null; (tempElement = decodeInputStream(dbis, nesting + 1)) != null;)
					tempList.add(tempElement);

				tempList.trimToSize();
				dbis.mark(0x7fffffff);
				tempByte = dbis.read();
				dbis.reset();
				if (nesting > 0 && tempByte == -1)
					throw new BEncodingException("BDecoder: invalid input data, 'e' missing from end of list");
			}
			catch (Throwable e)
			{
				if (!recovery_mode)
					if (e instanceof IOException)
						throw (IOException)e;
					else
						throw new IOException(Debug.getNestedExceptionMessage(e));
			}
			return tempList;

		case -1: 
		case 101: // 'e'
			return null;

		case 105: // 'i'
			return new Long(getNumberFromStream(dbis, 'e'));

		case 48: // '0'
		case 49: // '1'
		case 50: // '2'
		case 51: // '3'
		case 52: // '4'
		case 53: // '5'
		case 54: // '6'
		case 55: // '7'
		case 56: // '8'
		case 57: // '9'
			dbis.reset();
			return getByteArrayFromStream(dbis);
		}
		int rem_len = dbis.available();
		if (rem_len > 256)
			rem_len = 256;
		byte rem_data[] = new byte[rem_len];
		dbis.read(rem_data);
		throw new BEncodingException((new StringBuilder()).append("BDecoder: unknown command '").append(tempByte).append(", remainder = ").append(new String(rem_data)).toString());
	}

	private long getNumberFromStream(BDecoderInputStream dbis, char parseChar)
		throws IOException
	{
		int tempByte = dbis.read();
		int pos = 0;
		for (; tempByte != parseChar && tempByte >= 0; tempByte = dbis.read())
		{
			numberChars[pos++] = (char)tempByte;
			if (pos == numberChars.length)
				throw new NumberFormatException((new StringBuilder()).append("Number too large: ").append(new String(numberChars, 0, pos)).append("...").toString());
		}

		if (tempByte < 0)
			return -1L;
		if (pos == 0)
			return 0L;
		else
			return parseLong(numberChars, 0, pos);
	}

	public static long parseLong(char chars[], int start, int length)
	{
		long result = 0L;
		boolean negative = false;
		int i = start;
		int max = start + length;
		if (length > 0)
		{
			long limit;
			if (chars[i] == '-')
			{
				negative = true;
				limit = 0x8000000000000000L;
				i++;
			} else
			{
				limit = 0x8000000000000001L;
			}
			if (i < max)
			{
				int digit = chars[i++] - 48;
				if (digit < 0 || digit > 9)
					throw new NumberFormatException(new String(chars, start, length));
				result = -digit;
			}
			long multmin = limit / 10L;
			while (i < max) 
			{
				int digit = chars[i++] - 48;
				if (digit < 0 || digit > 9)
					throw new NumberFormatException(new String(chars, start, length));
				if (result < multmin)
					throw new NumberFormatException(new String(chars, start, length));
				result *= 10L;
				if (result < limit + (long)digit)
					throw new NumberFormatException(new String(chars, start, length));
				result -= digit;
			}
		} else
		{
			throw new NumberFormatException(new String(chars, start, length));
		}
		if (negative)
		{
			if (i > start + 1)
				return result;
			else
				throw new NumberFormatException(new String(chars, start, length));
		} else
		{
			return -result;
		}
	}

	private byte[] getByteArrayFromStream(BDecoderInputStream dbis)
		throws IOException
	{
		int length = (int)getNumberFromStream(dbis, ':');
		if (length < 0)
			return null;
		if (length > 0x800000)
			throw new IOException((new StringBuilder()).append("Byte array length too large (").append(length).append(")").toString());
		byte tempArray[] = new byte[length];
		int count = 0;
		for (int len = 0; count != length && (len = dbis.read(tempArray, count, length - count)) > 0; count += len);
		if (count != tempArray.length)
			throw new IOException("BDecoder::getByteArrayFromStream: truncated");
		else
			return tempArray;
	}

	public void setRecoveryMode(boolean r)
	{
		recovery_mode = r;
	}

	public static void print(Object obj)
	{
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		print(pw, obj);
		pw.flush();
		System.out.println(sw.toString());
	}

	public static void print(PrintWriter writer, Object obj)
	{
		print(writer, obj, "", false);
	}

	private static void print(PrintWriter writer, Object obj, String indent, boolean skip_indent)
	{
		String use_indent = skip_indent ? "" : indent;
		if (obj instanceof Long)
			writer.println((new StringBuilder()).append(use_indent).append(obj).toString());
		else
		if (obj instanceof byte[])
		{
			byte b[] = (byte[])(byte[])obj;
			if (b.length == 20)
				writer.println((new StringBuilder()).append(use_indent).append(" { ").append(ByteFormatter.nicePrint(b)).append(" }").toString());
			else
			if (b.length < 64)
				writer.println((new StringBuilder()).append(new String(b)).append(" [").append(ByteFormatter.encodeString(b)).append("]").toString());
			else
				writer.println((new StringBuilder()).append("[byte array length ").append(b.length).toString());
		} else
		if (obj instanceof String)
			writer.println((new StringBuilder()).append(use_indent).append(obj).toString());
		else
		if (obj instanceof List)
		{
			List l = (List)obj;
			writer.println((new StringBuilder()).append(use_indent).append("[").toString());
			for (int i = 0; i < l.size(); i++)
			{
				writer.print((new StringBuilder()).append(indent).append("  (").append(i).append(") ").toString());
				print(writer, l.get(i), (new StringBuilder()).append(indent).append("    ").toString(), true);
			}

			writer.println((new StringBuilder()).append(indent).append("]").toString());
		} else
		{
			Map m = (Map)obj;
			String key;
			for (Iterator it = m.keySet().iterator(); it.hasNext(); print(writer, m.get(key), (new StringBuilder()).append(indent).append("  ").toString(), true))
			{
				key = (String)it.next();
				if (key.length() > 256)
					writer.print((new StringBuilder()).append(indent).append(key.substring(0, 256)).append("... = ").toString());
				else
					writer.print((new StringBuilder()).append(indent).append(key).append(" = ").toString());
			}

		}
	}

	public static Map decodeStrings(Map map)
	{
		if (map == null)
			return null;
		Iterator it = map.entrySet().iterator();
		do
		{
			if (!it.hasNext())
				break;
			java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
			Object value = entry.getValue();
			if (value instanceof byte[])
				try
				{
					entry.setValue(new String((byte[])(byte[])value, "UTF-8"));
				}
				catch (Throwable e)
				{
					Debug.printStackTrace(e);
				}
			else
			if (value instanceof Map)
				decodeStrings((Map)value);
			else
			if (value instanceof List)
				decodeStrings((List)value);
		} while (true);
		return map;
	}

	public static List decodeStrings(List list)
	{
		if (list == null)
			return null;
		for (int i = 0; i < list.size(); i++)
		{
			Object value = list.get(i);
			if (value instanceof byte[])
			{
				try
				{
					String str = new String((byte[])(byte[])value, "UTF-8");
					list.set(i, str);
				}
				catch (Throwable e)
				{
					Debug.printStackTrace(e);
				}
				continue;
			}
			if (value instanceof Map)
			{
				decodeStrings((Map)value);
				continue;
			}
			if (value instanceof List)
				decodeStrings((List)value);
		}

		return list;
	}

	private static void print(File f, File output)
	{
		try
		{
			BDecoder decoder = new BDecoder();
			decoder.setRecoveryMode(false);
			PrintWriter pw = new PrintWriter(new FileWriter(output));
			print(pw, decoder.decodeStream(new BufferedInputStream(new FileInputStream(f))));
			pw.flush();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String args[])
	{
		print(new File("C:\\Temp\\tables.config"), new File("C:\\Temp\\tables.txt"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -