Many small fixes
This commit is contained in:
parent
8a930b361d
commit
9a0142c06c
18 changed files with 376 additions and 499 deletions
|
|
@ -29,13 +29,13 @@ import java.util.ArrayList;
|
|||
public class DynamicByteArrayStream extends InputStream{
|
||||
/** The byte array container */
|
||||
private ArrayList<byte[]> bytes;
|
||||
/** The current size of the stream */
|
||||
/** Current virtual size of the stream */
|
||||
private int size;
|
||||
/** points to the current index in the ArrayList */
|
||||
private int byteArrayIndex;
|
||||
/** points locally in the current index in the ArrayList */
|
||||
private int localPointer;
|
||||
/** The current position */
|
||||
/** Points the current byte array index */
|
||||
private int arrayIndex;
|
||||
/** Points to a local index in the current byte array */
|
||||
private int arrayLocalIndex;
|
||||
/** Current virtual position of the stream */
|
||||
private int pos;
|
||||
|
||||
/**
|
||||
|
|
@ -44,8 +44,8 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
public DynamicByteArrayStream(){
|
||||
bytes = new ArrayList<byte[]>();
|
||||
size = 0;
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
arrayIndex = 0;
|
||||
arrayLocalIndex = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
|
||||
/**
|
||||
* Append an byte array to the stream.
|
||||
* WARNING: This function will copy data.
|
||||
* NOTE: This function will copy data.
|
||||
*
|
||||
* @param b is the byte array to add
|
||||
* @param offset is the offset in the byte array
|
||||
|
|
@ -70,7 +70,7 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
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);
|
||||
bytes.add(new_b);
|
||||
size += length;
|
||||
}
|
||||
|
||||
|
|
@ -78,12 +78,12 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
public synchronized int read() throws IOException {
|
||||
if(pos >= size) return -1;
|
||||
|
||||
int ret = bytes.get(byteArrayIndex)[localPointer] & 0xff;
|
||||
int ret = bytes.get(arrayIndex)[arrayLocalIndex] & 0xff;
|
||||
pos++;
|
||||
localPointer++;
|
||||
if(localPointer >= bytes.get(byteArrayIndex).length){
|
||||
byteArrayIndex++;
|
||||
localPointer = 0;
|
||||
arrayLocalIndex++;
|
||||
if(arrayLocalIndex >= bytes.get(arrayIndex).length){
|
||||
arrayIndex++;
|
||||
arrayLocalIndex = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -91,27 +91,27 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
public synchronized int read(byte b[], int off, int len) {
|
||||
if(len <= 0) return 0;
|
||||
if(pos >= size) return -1;
|
||||
|
||||
int bytes_read = 0;
|
||||
|
||||
int bytes_read=0;
|
||||
if(pos+len >= size) len = size - pos;
|
||||
for(int i=0; i<len ;i++){
|
||||
byte[] src = bytes.get(byteArrayIndex);
|
||||
if(localPointer+len-i >= src.length){
|
||||
int length = src.length-localPointer;
|
||||
System.arraycopy(src, localPointer, b, off+i, length);
|
||||
for(; bytes_read<len ;bytes_read++){
|
||||
byte[] src = bytes.get(arrayIndex);
|
||||
// Read length is LONGER than local array
|
||||
if(arrayLocalIndex +len-bytes_read >= src.length){
|
||||
int length = src.length- arrayLocalIndex;
|
||||
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
|
||||
|
||||
localPointer = 0;
|
||||
byteArrayIndex++;
|
||||
arrayLocalIndex = 0;
|
||||
arrayIndex++;
|
||||
bytes_read += length;
|
||||
i += length;
|
||||
}
|
||||
// Read length is SHORTER than local array
|
||||
else{
|
||||
int length = len-i;
|
||||
System.arraycopy(src, localPointer, b, off+i, length);
|
||||
int length = len-bytes_read;
|
||||
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
|
||||
|
||||
localPointer += length;
|
||||
arrayLocalIndex += length;
|
||||
bytes_read += length;
|
||||
i += length;
|
||||
}
|
||||
}
|
||||
pos += len;
|
||||
|
|
@ -132,8 +132,8 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
}
|
||||
|
||||
public synchronized void reset() {
|
||||
byteArrayIndex = 0;
|
||||
localPointer = 0;
|
||||
arrayIndex = 0;
|
||||
arrayLocalIndex = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
|
|
@ -144,7 +144,7 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
/**
|
||||
* @return all of the buffers content as a byte array.
|
||||
*/
|
||||
public byte[] getByte(){
|
||||
public byte[] getBytes(){
|
||||
byte[] data = new byte[size];
|
||||
this.read(data, 0, size);
|
||||
return data;
|
||||
|
|
@ -154,10 +154,9 @@ public class DynamicByteArrayStream extends InputStream{
|
|||
/**
|
||||
* WARNING: This function might return a malformed String.
|
||||
*
|
||||
* @return all of the buffers content as a String.
|
||||
* @return all the contents of the buffers as a String.
|
||||
*/
|
||||
public String getString(){
|
||||
String data = new String( this.getByte() );
|
||||
return data;
|
||||
public String toString(){
|
||||
return new String( this.getBytes() );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,10 +22,7 @@
|
|||
|
||||
package zutil.io;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Utility class for streams and general IO stuff
|
||||
|
|
@ -36,23 +33,30 @@ import java.io.InputStreamReader;
|
|||
public class IOUtil {
|
||||
|
||||
/**
|
||||
* Reads and returns the content of a file as a String.
|
||||
* Or use FileUtils.readFileToString(file);
|
||||
* Reads and returns all the contents of a stream.
|
||||
*
|
||||
* @param stream is the file stream to read
|
||||
* @return The file content
|
||||
* @throws IOException
|
||||
* @param stream
|
||||
* @return the stream contents
|
||||
*/
|
||||
public static String getContent(InputStream stream) throws IOException{
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
|
||||
StringBuffer ret = new StringBuffer();
|
||||
int tmp;
|
||||
|
||||
while((tmp=in.read()) != -1){
|
||||
ret.append((char)tmp);
|
||||
public static byte[] getContent(InputStream stream) throws IOException{
|
||||
DynamicByteArrayStream dyn_buff = new DynamicByteArrayStream();
|
||||
byte[] buff = new byte[256];
|
||||
int len = 0;
|
||||
while((len = stream.read(buff)) != -1){
|
||||
dyn_buff.append(buff, 0, len);
|
||||
}
|
||||
|
||||
in.close();
|
||||
return ret.toString();
|
||||
|
||||
return dyn_buff.getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all data from the input stream to the output stream
|
||||
*/
|
||||
public static void copyStream(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buff = new byte[256];
|
||||
int len;
|
||||
while((len = in.read(buff)) > 0){
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,12 +22,9 @@
|
|||
|
||||
package zutil.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Reader;
|
||||
import zutil.Dumpable;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -35,8 +32,6 @@ import java.util.Collection;
|
|||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import zutil.Dumpable;
|
||||
|
||||
/**
|
||||
* @author Ziver
|
||||
* this class can print strings to multiple PrintStreams
|
||||
|
|
@ -71,7 +66,7 @@ public class MultiPrintStream extends PrintStream {
|
|||
|
||||
/**
|
||||
* This constructor takes a array of PrintStreams to be used
|
||||
* @param streams is a array of the streams that will be used
|
||||
* @param streams is a array of the streams that will be used
|
||||
*/
|
||||
public MultiPrintStream(PrintStream[] streams){
|
||||
super(streams[0]);
|
||||
|
|
@ -83,7 +78,7 @@ public class MultiPrintStream extends PrintStream {
|
|||
|
||||
/**
|
||||
* This constructor takes a array of PrintStreams to be used
|
||||
* @param streams is a array of the streams that will be used
|
||||
* @param instanceStream is a array of the streams that will be used
|
||||
*/
|
||||
public static void makeInstance(MultiPrintStream instanceStream){
|
||||
out = instanceStream;
|
||||
|
|
@ -99,7 +94,7 @@ public class MultiPrintStream extends PrintStream {
|
|||
|
||||
/**
|
||||
* Remove a PrintStream from the list
|
||||
* @param p is the PrintStream to remove
|
||||
* @param p is the PrintStream to remove
|
||||
*/
|
||||
public void removePrintStream(PrintStream p){
|
||||
streams.remove(p);
|
||||
|
|
@ -107,7 +102,7 @@ public class MultiPrintStream extends PrintStream {
|
|||
|
||||
/**
|
||||
* Remove a PrintStream from the list
|
||||
* @param p is the index of the PrintStream to remove
|
||||
* @param p is the index of the PrintStream to remove
|
||||
*/
|
||||
public void removePrintStream(int p){
|
||||
streams.remove(p);
|
||||
|
|
@ -206,8 +201,8 @@ public class MultiPrintStream extends PrintStream {
|
|||
* <br>- Reader content (Prints out until the end of the reader)
|
||||
* <br>- Instance variables of a Object
|
||||
*
|
||||
* @param o is the Object to dump
|
||||
* @return A String with all the printed data
|
||||
* @param o is the Object to dump
|
||||
* @return a String with all the printed data
|
||||
*/
|
||||
public String dumpToString( Object o) {
|
||||
return dumpToString(o, "");
|
||||
|
|
@ -222,8 +217,8 @@ public class MultiPrintStream extends PrintStream {
|
|||
* <br>- Reader content (Prints out until the end of the reader)
|
||||
* <br>- Instance variables of a Object
|
||||
*
|
||||
* @param o is the Object to dump
|
||||
* @param head is the string that will be put in front of every line
|
||||
* @param o is the Object to dump
|
||||
* @param head is the string that will be put in front of every line
|
||||
* @return A String with all the printed data
|
||||
*/
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ import zutil.log.LogUtil;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class FileUtil {
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
/**
|
||||
* Returns a String with a relative path from the given path
|
||||
|
|
@ -123,24 +123,28 @@ public class FileUtil {
|
|||
* Reads and returns the content of a file as a String.
|
||||
* Or use FileUtils.readFileToString(file);
|
||||
*
|
||||
* @param file is the file to read
|
||||
* @return The file content
|
||||
* @throws IOException
|
||||
* @param file
|
||||
* @return the file content
|
||||
*/
|
||||
public static String getFileContent(File file) throws IOException{
|
||||
return IOUtil.getContent( new FileInputStream(file) );
|
||||
InputStream in = new FileInputStream(file);
|
||||
String data = new String(IOUtil.getContent( in ));
|
||||
in.close();
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and returns the content of a file as a String.
|
||||
* Or use FileUtils.readFileToString(file);
|
||||
*
|
||||
* @param url is the url to read
|
||||
* @return The file content
|
||||
* @throws IOException
|
||||
* @param url
|
||||
* @return the file content
|
||||
*/
|
||||
public static String getContent(URL url) throws IOException{
|
||||
return IOUtil.getContent( url.openStream() );
|
||||
InputStream in = url.openStream();
|
||||
String data = new String(IOUtil.getContent( in ));
|
||||
in.close();
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue