From 69a5efec970fae7306b64472d685a0e8903841bc Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 1 May 2012 18:23:28 +0000 Subject: [PATCH] Changed function names and added some new functions --- src/zutil/io/DynamicByteArrayStream.java | 47 +++++++++++++++++++----- src/zutil/net/nio/NioNetwork.java | 2 +- src/zutil/parser/Base64Decoder.java | 44 ++++++++++++++++++---- 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/zutil/io/DynamicByteArrayStream.java b/src/zutil/io/DynamicByteArrayStream.java index cdf59e5..a67db46 100644 --- a/src/zutil/io/DynamicByteArrayStream.java +++ b/src/zutil/io/DynamicByteArrayStream.java @@ -50,12 +50,28 @@ public class DynamicByteArrayStream extends InputStream{ /** * Append an byte array to the stream - * @param b The byte array to add + * + * @param b is the byte array to add. */ - public synchronized void add(byte[] b){ + public synchronized void append(byte[] b){ bytes.add(b); size += b.length; } + + /** + * Append an byte array to the stream. + * WARNING: This function will copy data. + * + * @param b is the byte array to add + * @param offset is the offset in the byte array + * @param length is the amount of data to add + */ + public synchronized void append(byte[] b, int offset, int length){ + byte[] new_b = new byte[length]; + System.arraycopy(b, offset, new_b, 0, length); + bytes.add(b); + size += length; + } @Override public synchronized int read() throws IOException { @@ -72,10 +88,6 @@ public class DynamicByteArrayStream extends InputStream{ } public synchronized int read(byte b[], int off, int len) { - //System.out.println("*****************************************************"); - //System.out.println("off: "+off+" len: "+len); - //System.out.println("size: "+size+" arraylen: "+bytes.size()); - //System.out.println("pos: "+pos+" localPointer: "+localPointer); if(len <= 0) return 0; if(pos >= size) return -1; @@ -84,7 +96,6 @@ public class DynamicByteArrayStream extends InputStream{ for(int i=0; i= src.length){ - //System.out.println("1"); int length = src.length-localPointer; System.arraycopy(src, localPointer, b, off+i, length); @@ -94,7 +105,6 @@ public class DynamicByteArrayStream extends InputStream{ i += length; } else{ - //System.out.println("2"); int length = len-i; System.arraycopy(src, localPointer, b, off+i, length); @@ -104,7 +114,6 @@ public class DynamicByteArrayStream extends InputStream{ } } pos += len; - //System.out.println("new_pos: "+pos+" read: "+bytes_read); return bytes_read; } @@ -130,4 +139,24 @@ public class DynamicByteArrayStream extends InputStream{ public void close() throws IOException { clear(); } + + /** + * @return all of the buffers content as a byte array. + */ + public byte[] getByte(){ + byte[] data = new byte[size]; + this.read(data, 0, size); + return data; + } + + + /** + * WARNING: This function might return a malformed String. + * + * @return all of the buffers content as a String. + */ + public String getString(){ + String data = new String( this.getByte() ); + return data; + } } diff --git a/src/zutil/net/nio/NioNetwork.java b/src/zutil/net/nio/NioNetwork.java index b2bda6e..d6e1f15 100644 --- a/src/zutil/net/nio/NioNetwork.java +++ b/src/zutil/net/nio/NioNetwork.java @@ -318,7 +318,7 @@ public abstract class NioNetwork implements Runnable { pendingReadData.put(socketChannel, new DynamicByteArrayStream()); } DynamicByteArrayStream dynBuf = pendingReadData.get(socketChannel); - dynBuf.add(rspByteData); + dynBuf.append(rspByteData); Object rspData = null; diff --git a/src/zutil/parser/Base64Decoder.java b/src/zutil/parser/Base64Decoder.java index 1231730..4813559 100644 --- a/src/zutil/parser/Base64Decoder.java +++ b/src/zutil/parser/Base64Decoder.java @@ -1,5 +1,8 @@ package zutil.parser; +import zutil.converters.Converter; +import zutil.io.DynamicByteArrayStream; + public class Base64Decoder { public static final char[] B64_ENCODE_TABLE = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', @@ -14,15 +17,27 @@ public class Base64Decoder { '9', '+', '/' }; - private StringBuilder output; + private DynamicByteArrayStream output; private byte rest_data; private int rest = 0; public Base64Decoder(){ - output = new StringBuilder(); + output = new DynamicByteArrayStream(); } - public void decode( String data ){ + public static String decodeToHex( String data ){ + Base64Decoder base64 = new Base64Decoder(); + base64.write( data ); + return Converter.toHexString( base64.getByte() ); + } + + public static String decode( String data ){ + Base64Decoder base64 = new Base64Decoder(); + base64.write( data ); + return base64.getString(); + } + + public void write( String data ){ byte[] buffer = new byte[ (data.length()*6/8) + 1 ]; int buffi = 0; if( rest != 0 ) @@ -58,19 +73,34 @@ public class Base64Decoder { if( rest != 0 ) rest_data = buffer[buffi--]; - output.append(new String(buffer, 0, buffi)); + output.append( buffer, 0, buffi ); } - public String toString(){ - return output.toString(); + public String getString(){ + return output.getString(); + } + + public byte[] getByte(){ + return output.getByte(); } public void reset(){ - output = new StringBuilder(); + output.reset(); 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 ){ switch(c){ case 'A': return (byte)( 0 & 0xff);