Changed function names and added some new functions
This commit is contained in:
parent
6e890b81e1
commit
69a5efec97
3 changed files with 76 additions and 17 deletions
|
|
@ -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<len ;i++){
|
||||
byte[] src = bytes.get(byteArrayIndex);
|
||||
if(localPointer+len-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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue