diff --git a/src/zutil/converters/Converter.java b/src/zutil/converters/Converter.java old mode 100644 new mode 100755 index ff19f6d..ea147c7 --- a/src/zutil/converters/Converter.java +++ b/src/zutil/converters/Converter.java @@ -170,22 +170,6 @@ public class Converter { return object; } - /** - * Checks if the given interface is implemented in the object - * - * @param object the object to look for the interface - * @param interf the interface to look for - * @return true if the interface is implemented else false - */ - public static boolean isInstanceOf(Object object, Class interf){ - Class[] objectInterf = object.getClass().getInterfaces(); - for(int i=0; i%2" - * where the %x corresponds to BBCode like this: [url=%1]%2[/url] + * @param bbcode is the BBCode e.g. "b" or "url" + * @param html is the corresponding HTML e.g. "%2" + * where the %x corresponds to BBCode like this: [url=%1]%2[/url] */ public void addBBCode(String bbcode, String html){ bbcodes.put(bbcode, html); @@ -70,7 +70,7 @@ public class BBCodeParser { /** * Removes a BBCode definition from the parser * - * @param bbcode is the bbcode to remove + * @param bbcode is the bbcode to remove */ public void removeBBCode(String bbcode){ bbcodes.remove(bbcode); @@ -79,7 +79,7 @@ public class BBCodeParser { /** * Parses the text with BBCode and converts it to HTML * - * @param text is a String with BBCode + * @param text is a String with BBCode * @return a String where all BBCode has been replaced by HTML */ public String read(String text){ @@ -149,7 +149,7 @@ public class BBCodeParser { /** * Parses a parameter from a BBCode block: [url=param] * - * @param text is the text to parse from + * @param text is the text to parse from * @return only the parameter string */ private String parseParam(MutableInt index, StringBuilder text){ @@ -168,7 +168,7 @@ public class BBCodeParser { /** * Parses the value in the BBCodes e.g. [url]value[/url] * - * @param text is the text to parse the value from + * @param text is the text to parse the value from */ private String parseValue(MutableInt index, StringBuilder text, String bbcode){ StringBuilder value = new StringBuilder(); diff --git a/src/zutil/parser/BEncodedParser.java b/src/zutil/parser/BEncodedParser.java old mode 100644 new mode 100755 index 979972b..449308e --- a/src/zutil/parser/BEncodedParser.java +++ b/src/zutil/parser/BEncodedParser.java @@ -37,7 +37,7 @@ public class BEncodedParser { /** * Returns the representation of the data in the BEncoded string * - * @param data The data to be decoded + * @param data is the data to be decoded * @return */ public static DataNode read(String data){ @@ -47,8 +47,8 @@ public class BEncodedParser { /** * Returns the representation of the data in the BEncoded string * - * @param data The data to be decoded - * @param index The index in data to start from + * @param data is the data to be decoded + * @param index is the index in data to start from * @return */ private static DataNode decode_BEncoded(MutableInt index, StringBuilder data){ @@ -62,11 +62,9 @@ public class BEncodedParser { * i-3272002e. */ case 'i': - //System.out.println("Found Integer at "+index); index.i++; tmp = data.substring(index.i, data.indexOf("e")); index.i += tmp.length() + 1; - //System.out.println(tmp); return new DataNode( new Long(tmp)); /** * Lists are prefixed with a l and terminated by an e. The list @@ -76,7 +74,6 @@ public class BEncodedParser { * would bEncode to li1e7:Mondunai3el3:Sub4:Listee */ case 'l': - //System.out.println("Found List at "+index); index.i++; DataNode list = new DataNode( DataType.List ); c = data.charAt(index.i); @@ -85,7 +82,6 @@ public class BEncodedParser { c = data.charAt(index.i); } index.i++; - //MultiPrintStream.out.dump(list); if(list.size() == 1) return list.get(0); else return list; /** @@ -95,7 +91,6 @@ public class BEncodedParser { * would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee */ case 'd': - //System.out.println("Found Dictionary at "+index); index.i++; DataNode map = new DataNode( DataType.Map ); c = data.charAt(index.i); @@ -105,7 +100,6 @@ public class BEncodedParser { c = data.charAt(index.i); } index.i++; - //MultiPrintStream.out.dump(map); return map; /** * Strings are prefixed with their length followed by a colon. @@ -113,13 +107,11 @@ public class BEncodedParser { * would bEncode to 11:BitTorrents. */ default: - //System.out.println("Found String at "+index); tmp = data.substring(index.i, data.indexOf(":")); int length = Integer.parseInt(tmp); index.i += tmp.length() + 1; String ret = data.substring(index.i, length); index.i += length; - //System.out.println(data.substring(i, i+length)); return new DataNode( ret ); } } diff --git a/src/zutil/parser/Base64Decoder.java b/src/zutil/parser/Base64Decoder.java old mode 100644 new mode 100755 index ff76ce6..ec82e20 --- a/src/zutil/parser/Base64Decoder.java +++ b/src/zutil/parser/Base64Decoder.java @@ -52,23 +52,23 @@ public class Base64Decoder { public static String decode( String data ){ Base64Decoder base64 = new Base64Decoder(); - base64.write( data ); + base64.read( data ); return base64.toString(); } public static String decodeToHex( String data ){ Base64Decoder base64 = new Base64Decoder(); - base64.write( data ); + base64.read( data ); return Converter.toHexString( base64.getByte() ); } public static byte[] decodeToByte( String data ){ Base64Decoder base64 = new Base64Decoder(); - base64.write( data ); + base64.read( data ); return base64.getByte(); } - public void write( String data ){ + public void read( String data ){ byte[] buffer = new byte[ (data.length()*6/8) + 1 ]; int buffi = 0; if( rest != 0 ) @@ -120,19 +120,9 @@ public class Base64Decoder { rest = 0; rest_data = 0; } + - public static String addPadding( String data ){ - int padding = 4 - (data.length() % 4); - switch( padding ){ - case 0: return data; - case 1: return data + "="; - case 2: return data + "=="; - case 3: return data + "==="; - } - return null; - } - - private byte getByte( char c ){ + private static byte getByte( char c ){ switch(c){ case 'A': return (byte)( 0 & 0xff); case 'B': return (byte)( 1 & 0xff); diff --git a/src/zutil/parser/Base64Encoder.java b/src/zutil/parser/Base64Encoder.java new file mode 100755 index 0000000..d42ad6d --- /dev/null +++ b/src/zutil/parser/Base64Encoder.java @@ -0,0 +1,162 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Ziver Koc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package zutil.parser; + + +public class Base64Encoder { + + public static String encode( byte[] data ){ + Base64Encoder base64 = new Base64Encoder(); + return base64.write( data ); + } + public static String encode( String data ){ + Base64Encoder base64 = new Base64Encoder(); + return base64.write( data.getBytes() ); + } + + private static String write( byte[] data ){ + char[] buffer = new char[getBufferLength(data.length)]; + int buffIndex = 0; + + int rest = 0; // how much rest we have + for( int i=0; i> 2) & 0b0011_1111); + break; + case 2: + b = (byte) ((data[i-1] << 4) & 0b0011_0000); + b |= (byte) ((data[i] >> 4) & 0b0000_1111); + break; + case 4: + b = (byte) ((data[i-1] << 2) & 0b0011_1100); + b |= (byte) ((data[i] >> 6) & 0b0000_0011); + break; + case 6: + --i; // Go back one element + b = (byte) (data[i] & 0b0011_1111); + break; + } + + rest = (rest + 2) % 8; + buffer[buffIndex++] = getChar(b); + } + // Any rest left? + if(rest == 2) + buffer[buffIndex++] = getChar((byte) ((data[data.length-1] << 4) & 0b0011_0000)); + else if(rest == 4) + buffer[buffIndex++] = getChar((byte) ((data[data.length-1] << 2) & 0b0011_1100)); + else if(rest == 6) + buffer[buffIndex++] = getChar((byte) (data[data.length-1] & 0b0011_1111)); + + // Add padding + for(; buffIndex