diff --git a/.classpath b/.classpath
index 0826fa7..ed6012c 100644
--- a/.classpath
+++ b/.classpath
@@ -7,18 +7,10 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
diff --git a/.idea/.name b/.idea/.name
deleted file mode 100644
index 00751cf..0000000
--- a/.idea/.name
+++ /dev/null
@@ -1 +0,0 @@
-ZUtil
\ No newline at end of file
diff --git a/.idea/ant.xml b/.idea/ant.xml
deleted file mode 100644
index db0112b..0000000
--- a/.idea/ant.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index 0a239b9..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/.idea/copyright/MIT.xml b/.idea/copyright/MIT.xml
deleted file mode 100644
index e7fb098..0000000
--- a/.idea/copyright/MIT.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
deleted file mode 100644
index 95c3e81..0000000
--- a/.idea/copyright/profiles_settings.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/dictionaries/ezivkoc.xml b/.idea/dictionaries/ezivkoc.xml
deleted file mode 100644
index f3ba707..0000000
--- a/.idea/dictionaries/ezivkoc.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
deleted file mode 100644
index 7c62b52..0000000
--- a/.idea/encodings.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-
-
-
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index a001a41..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,45 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index 8e53d9a..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml
deleted file mode 100644
index 0d5175c..0000000
--- a/.idea/scopes/scope_settings.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
deleted file mode 100644
index 1e7cce4..0000000
--- a/.idea/uiDesigner.xml
+++ /dev/null
@@ -1,125 +0,0 @@
-
-
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
-
-
-
-
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index cce6fd9..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs
deleted file mode 100644
index 139d88b..0000000
--- a/.settings/org.eclipse.jdt.core.prefs
+++ /dev/null
@@ -1,8 +0,0 @@
-#Mon Aug 16 22:05:56 CEST 2010
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-org.eclipse.jdt.core.compiler.compliance=1.5
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.5
diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml
index 054be2e..126f829 100644
--- a/.settings/org.eclipse.wst.common.project.facet.core.xml
+++ b/.settings/org.eclipse.wst.common.project.facet.core.xml
@@ -1,6 +1,5 @@
-
diff --git a/libs/sqlite-jdbc-3.7.2.jar b/libs/sqlite-jdbc-3.7.2.jar
new file mode 100644
index 0000000..b0bec7b
Binary files /dev/null and b/libs/sqlite-jdbc-3.7.2.jar differ
diff --git a/src/zutil/db/DBConnection.java b/src/zutil/db/DBConnection.java
index 0f2ea69..bbfbc12 100644
--- a/src/zutil/db/DBConnection.java
+++ b/src/zutil/db/DBConnection.java
@@ -44,7 +44,8 @@ public class DBConnection implements Closeable{
private static final Logger logger = LogUtil.getLogger();
public enum DBMS{
- MySQL
+ MySQL,
+ SQLite
}
// The connection
private Connection conn = null;
@@ -88,6 +89,17 @@ public class DBConnection implements Closeable{
String dbms_name = initDriver(dbms);
conn = DriverManager.getConnection ("jdbc:"+dbms_name+"://"+url+"/"+db, user, password);
}
+
+ /**
+ * Creates an Connection to a DB file
+ *
+ * @param dbms is the DB type
+ * @param db is the database to connect to
+ */
+ public DBConnection(DBMS dbms, String db) throws Exception{
+ String dbms_name = initDriver(dbms);
+ conn = DriverManager.getConnection ("jdbc:"+dbms_name+":"+db);
+ }
/**
* @return the underlying connection
@@ -105,11 +117,15 @@ public class DBConnection implements Closeable{
public String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
switch(db){
case MySQL:
- Class.forName ("com.mysql.jdbc.Driver").newInstance();
+ Class.forName("com.mysql.jdbc.Driver").newInstance();
DriverManager.setLoginTimeout(10);
return "mysql";
+ case SQLite:
+ Class.forName("org.sqlite.JDBC");
+ return "sqlite";
+ default:
+ return null;
}
- return null;
}
/**
@@ -162,16 +178,23 @@ public class DBConnection implements Closeable{
* @return update count or -1 if the query is not an update query
*/
public static int exec(PreparedStatement stmt) throws SQLException {
- return exec(stmt, new SQLResultHandler(){
+ Integer ret = exec(stmt, new SQLResultHandler(){
public Integer handleQueryResult(Statement stmt, ResultSet result) {
try {
- return stmt.getUpdateCount();
+ if(stmt != null)
+ return stmt.getUpdateCount();
+ else
+ return -1;
} catch (SQLException e) {
logger.log(Level.WARNING, null, e);
}
return -1;
}
});
+
+ if(ret != null)
+ return ret;
+ return -1;
}
/**
@@ -203,23 +226,35 @@ public class DBConnection implements Closeable{
if( handler != null ){
ResultSet result = null;
try{
- result = stmt.getResultSet();
- return handler.handleQueryResult(stmt, result);
+ if(stmt.getMoreResults()){
+ result = stmt.getResultSet();
+ return handler.handleQueryResult(stmt, result);
+ }
+ else
+ return null;
+ }catch(SQLException sqlex){
+ logger.throwing(null, null, sqlex);
}finally{
if(result != null){
try {
result.close();
- } catch (SQLException e) { }
+ } catch (SQLException sqlex) {
+ logger.throwing(null, null, sqlex);
+ }
result = null;
}
}
}
+ }catch(SQLException sqlex){
+ logger.throwing(null, null, sqlex);
// Cleanup
} finally {
if (stmt != null) {
try {
stmt.close();
- } catch (SQLException sqlex) { }
+ } catch (SQLException sqlex) {
+ logger.throwing(null, null, sqlex);
+ }
stmt = null;
}
}
diff --git a/src/zutil/db/bean/DBBean.java b/src/zutil/db/bean/DBBean.java
index 8830362..5970df0 100644
--- a/src/zutil/db/bean/DBBean.java
+++ b/src/zutil/db/bean/DBBean.java
@@ -43,12 +43,13 @@ import java.util.logging.Logger;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
-/**
- *
- * The class that extends this will be able to save its state to a DB.
+/**
+ * The class that extends this will be able to save its state to a database.
* Fields that are transient will be ignored, and fields that extend
- * DBBean will be replaced with the id field of that class in the database.
+ * DBBean will be replaced with the an id which corresponds to the field
+ * of that object.
*
+ *
* Supported fields:
* *Boolean
* *Integer
@@ -59,8 +60,7 @@ import zutil.log.LogUtil;
* *Character
* *DBBean
* *java.sql.Timestamp
- * *List
- *
+ * *List
*
* @author Ziver
*/
@@ -340,11 +340,11 @@ public abstract class DBBean {
}
/**
- * Loads all the rows in the table into a LinkedList
+ * Loads all rows from the table into a LinkedList
*
* @param is the class of the bean
* @param c is the class of the bean
- * @return a LinkedList with all the Beans in the DB
+ * @return a LinkedList with all the beans in the DB
*/
public static List load(DBConnection db, Class c) throws SQLException {
// Initiate a BeanConfig if there is non
@@ -359,12 +359,12 @@ public abstract class DBBean {
}
/**
- * Loads all the rows in the table into a LinkedList
+ * Loads a specific instance of the bean from the table with the specific id
*
* @param is the class of the bean
* @param c is the class of the bean
* @param id is the id value of the bean
- * @return a DBBean Object with the specific id or null
+ * @return a DBBean Object with the specific id or null if the id was not found
*/
public static T load(DBConnection db, Class c, long id) throws SQLException {
// Initiate a BeanConfig if there is non
diff --git a/src/zutil/io/DynamicByteArrayStream.java b/src/zutil/io/DynamicByteArrayStream.java
index 8bc1a6a..d3400fe 100644
--- a/src/zutil/io/DynamicByteArrayStream.java
+++ b/src/zutil/io/DynamicByteArrayStream.java
@@ -29,24 +29,25 @@ import java.util.ArrayList;
public class DynamicByteArrayStream extends InputStream{
/** The byte array container */
private ArrayList bytes;
- /** Current virtual size of the stream */
- private int size;
- /** 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;
+ private int globalPos;
+ /** Current virtual size of the stream */
+ private int globalSize;
+ /** Points the current byte array index */
+ private int globalArrayIndex;
+ /** Points to a local index in the current byte array */
+ private int localArrayOffset;
+
/**
* Create a new instance of DynamicByteArrayStream
*/
public DynamicByteArrayStream(){
bytes = new ArrayList();
- size = 0;
- arrayIndex = 0;
- arrayLocalIndex = 0;
- pos = 0;
+ globalPos = 0;
+ globalSize = 0;
+ globalArrayIndex = 0;
+ localArrayOffset = 0;
}
/**
@@ -56,7 +57,7 @@ public class DynamicByteArrayStream extends InputStream{
*/
public synchronized void append(byte[] b){
bytes.add(b);
- size += b.length;
+ globalSize += b.length;
}
/**
@@ -71,70 +72,70 @@ public class DynamicByteArrayStream extends InputStream{
byte[] new_b = new byte[length];
System.arraycopy(b, offset, new_b, 0, length);
bytes.add(new_b);
- size += length;
+ globalSize += length;
}
@Override
public synchronized int read() throws IOException {
- if(pos >= size) return -1;
+ if(globalPos >= globalSize) return -1;
- int ret = bytes.get(arrayIndex)[arrayLocalIndex] & 0xff;
- pos++;
- arrayLocalIndex++;
- if(arrayLocalIndex >= bytes.get(arrayIndex).length){
- arrayIndex++;
- arrayLocalIndex = 0;
+ int ret = bytes.get(globalArrayIndex)[localArrayOffset] & 0xff;
+ globalPos++;
+ localArrayOffset++;
+ if(localArrayOffset >= bytes.get(globalArrayIndex).length){
+ globalArrayIndex++;
+ localArrayOffset = 0;
}
return ret;
}
public synchronized int read(byte b[], int off, int len) {
if(len <= 0) return 0;
- if(pos >= size) return -1;
+ if(globalPos >= globalSize) return -1;
int bytes_read=0;
- if(pos+len >= size) len = size - pos;
- for(; bytes_read= globalSize) len = globalSize - globalPos;
+ while(bytes_read= src.length){
- int length = src.length- arrayLocalIndex;
- System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
+ if(localArrayOffset +len-bytes_read > src.length){
+ int length = src.length- localArrayOffset;
+ System.arraycopy(src, localArrayOffset, b, off+bytes_read, length);
- arrayLocalIndex = 0;
- arrayIndex++;
+ localArrayOffset = 0;
+ globalArrayIndex++;
bytes_read += length;
}
// Read length is SHORTER than local array
else{
int length = len-bytes_read;
- System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
+ System.arraycopy(src, localArrayOffset, b, off+bytes_read, length);
- arrayLocalIndex += length;
+ localArrayOffset += length;
bytes_read += length;
}
}
- pos += len;
+ globalPos += len;
return bytes_read;
}
public synchronized int available() {
- return size - pos;
+ return globalSize - globalPos;
}
/**
* Clears this stream from the byte arrays
*/
public synchronized void clear(){
- size = 0;
+ globalSize = 0;
reset();
bytes.clear();
}
public synchronized void reset() {
- arrayIndex = 0;
- arrayLocalIndex = 0;
- pos = 0;
+ globalArrayIndex = 0;
+ localArrayOffset = 0;
+ globalPos = 0;
}
public void close() throws IOException {
@@ -145,8 +146,8 @@ public class DynamicByteArrayStream extends InputStream{
* @return all of the buffers content as a byte array.
*/
public byte[] getBytes(){
- byte[] data = new byte[size];
- this.read(data, 0, size);
+ byte[] data = new byte[globalSize];
+ this.read(data, 0, globalSize);
return data;
}
diff --git a/src/zutil/io/InputStreamCloser.java b/src/zutil/io/InputStreamCloser.java
new file mode 100644
index 0000000..f03b1b0
--- /dev/null
+++ b/src/zutil/io/InputStreamCloser.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Ziver
+ *
+ * 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.io;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * A simple Class that mirrors a InputStream but
+ * also has an additional Closeable object that
+ * will be closed with the InputStream
+ *
+ * @author Ziver
+ */
+public class InputStreamCloser extends InputStream{
+ private Closeable c;
+ private InputStream in;
+
+ public InputStreamCloser(InputStream in, Closeable c){
+ this.c = c;
+ this.in = in;
+ }
+
+ public void close() throws IOException {
+ in.close();
+ c.close();
+ }
+
+ // Mirror functions
+ public int read() throws IOException { return in.read(); }
+ public int read(byte b[]) throws IOException { return in.read(b); }
+ public int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); }
+ public long skip(long n) throws IOException { return in.skip(n); }
+ public int available() throws IOException { return in.available(); }
+ public synchronized void mark(int readlimit) { in.mark(readlimit); }
+ public synchronized void reset() throws IOException { in.reset(); }
+ public boolean markSupported() { return in.markSupported(); }
+}
\ No newline at end of file
diff --git a/src/zutil/io/file/FileSearch.java b/src/zutil/io/file/FileSearch.java
index 7a1dd60..1b9e75b 100644
--- a/src/zutil/io/file/FileSearch.java
+++ b/src/zutil/io/file/FileSearch.java
@@ -23,49 +23,68 @@
package zutil.io.file;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
import java.util.Iterator;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
-public class FileSearch implements Iterable{
+import zutil.io.InputStreamCloser;
+
+public class FileSearch implements Iterable{
+ // Constants
+ private static final List compressedFileExtensions = Arrays.asList(new String[]{
+ "jar", "zip"
+ });
+
+ // Constructor params
private File root;
-
+
// Search parameters
private String fileName;
private String extension;
private boolean recursive;
//private int depth;
private boolean searchFiles;
+ private boolean searchCompressedFiles;
private boolean searchFolders;
-
-
+
+
public FileSearch(File root){
this.root = root;
searchFiles = true;
searchFolders = true;
}
-
-
+
+
/**
* @param file Sets the exact file name to search for (includes extension)
*/
public void setFileName(String file){
fileName = file;
}
-
+
/**
* Sets the file extensions to search for (should not include . at the beggining)
*/
public void setExtension(String ext){
-
+
}
-
+
/**
* Sets if the search should go into sub-folders
*/
public void setRecursive(boolean recursive){
this.recursive = recursive;
}
-
+
/**
* Sets how deep into folders the search should go
* (Recursion needs to be enabled for this attribute to be used)
@@ -73,75 +92,153 @@ public class FileSearch implements Iterable{
//public void setDepth(int depth){
// this.depth = depth;
//}
-
+
public void searchFiles(boolean searchFiles){
this.searchFiles = searchFiles;
}
+ public void searchCompressedFiles(boolean searchCompressedFiles){
+ this.searchCompressedFiles = searchCompressedFiles;
+ }
public void searchFolders(boolean searchFolders){
this.searchFolders = searchFolders;
}
-
+
@Override
- public Iterator iterator() {
+ public Iterator iterator() {
return new FileSearchIterator();
}
-
-
- protected class FileSearchIterator implements Iterator{
- private ArrayList fileList;
+
+
+ protected class FileSearchIterator implements Iterator{
+ private ArrayList fileList;
private int currentIndex;
public FileSearchIterator(){
- fileList = new ArrayList();
+ fileList = new ArrayList();
currentIndex = 0;
-
- addToFileList(root.listFiles());
+
+ addFiles(root.list());
next();
}
-
+
@Override
public boolean hasNext() {
return currentIndex != fileList.size();
}
-
+
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
- public File next() {
+ public FileSearchItem next() {
if(currentIndex < 0)
return null;
// Temporarily save the current file
- File ret = fileList.get(currentIndex);
-
+ FileSearchItem ret = fileList.get(currentIndex);
+
// Find the next file
for(; currentIndex e = zipFile.entries();
+ while(e.hasMoreElements()){
+ ZipEntry entry = e.nextElement();
+ fileList.add(new FileSearchZipItem(url, entry));
+ }
+ zipFile.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
else if(searchFiles && file.isFile()){
- if(extension != null && FileUtil.getFileExtension(file).equalsIgnoreCase(extension))
+ if(extension != null && FileUtil.getFileExtension(file.getName()).equalsIgnoreCase(extension))
break;
else if(fileName != null && file.getName().equalsIgnoreCase(fileName))
break;
}
}
-
+
return ret;
}
-
- private void addToFileList(File[] list){
- for(File file : list){
- fileList.add(file);
+
+ private void addFiles(String[] list){
+ for(String file : list){
+ fileList.add(new FileSearchFileItem(new File(file)));
}
}
-
+
}
}
+
+
+interface FileSearchItem{
+ /** @return a file or folder name **/
+ public String getName();
+ /** @return a URL to the file or folder, in case of a compressed file the URL to the package will be returned **/
+ public URL getUrl() throws MalformedURLException ;
+
+ public boolean isCompressed();
+ public boolean isFile();
+ public boolean isDirectory();
+
+ /** @return an InputStream if this is a file otherwise null **/
+ public InputStream getInputStream() throws IOException;
+ /** @return an String array with all files if this is a folder otherwise null **/
+ public String[] listFiles();
+}
+
+
+class FileSearchFileItem implements FileSearchItem{
+ private File file;
+
+ protected FileSearchFileItem(File file){
+ this.file = file;
+ }
+
+ public String getName() { return file.getName(); }
+ public URL getUrl() throws MalformedURLException { return new URL(file.getAbsolutePath()); }
+
+ public boolean isCompressed() { return false; }
+ public boolean isFile() { return file.isFile(); }
+ public boolean isDirectory() { return file.isDirectory(); }
+
+ public InputStream getInputStream() throws IOException { return new FileInputStream(file); }
+ public String[] listFiles() { return file.list(); }
+
+}
+
+class FileSearchZipItem implements FileSearchItem{
+ private String file;
+ private ZipEntry entry;
+
+ protected FileSearchZipItem(String file, ZipEntry entry){
+ this.file = file;
+ this.entry = entry;
+ }
+
+ public String getName() { return entry.getName(); }
+ public URL getUrl() throws MalformedURLException { return new URL(file); }
+
+ public boolean isCompressed() { return true; }
+ public boolean isFile() { return !entry.isDirectory(); }
+ public boolean isDirectory() { return entry.isDirectory(); }
+
+ public InputStream getInputStream() throws IOException {
+ ZipFile zip = new ZipFile(file);
+ return new InputStreamCloser(zip.getInputStream(entry), zip);
+ }
+ public String[] listFiles() { return null; }
+
+}
\ No newline at end of file
diff --git a/src/zutil/io/file/FileUtil.java b/src/zutil/io/file/FileUtil.java
index 6e2095a..e0bcb36 100644
--- a/src/zutil/io/file/FileUtil.java
+++ b/src/zutil/io/file/FileUtil.java
@@ -23,8 +23,12 @@
package zutil.io.file;
import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
@@ -310,4 +314,43 @@ public class FileUtil {
return file.substring(0, file.lastIndexOf(".")+1)+ext;
}
+ /**
+ * This function will replace some data between two boundaries.
+ * If the boundary is not found it will be added to the end of the file.
+ *
+ * @param file is the file to modify
+ * @param boundary is the start and end boundary to put the data between, this is a full line boundary.
+ * @param data is the data that will be written to the file
+ */
+ public static void writeBetweenBoundary(File file, String boundary, String data) throws IOException{
+ BufferedReader in = new BufferedReader(new FileReader(file));
+ StringBuilder output = new StringBuilder();
+
+ String line;
+ while((line = in.readLine()) != null){
+ // Found starting boundary
+ if(line.equals(boundary)){
+ while((line = in.readLine()) != null)
+ // Find ending boundary
+ if(line.equals(boundary)) break;
+ // EOF and no ending boundary found
+ if(line == null){
+ in.close();
+ throw new EOFException("No ending boundary found");
+ }
+ // Write the new data
+ output.append(boundary).append('\n');
+ output.append(data).append('\n');
+ output.append(boundary).append('\n');
+ }
+ else
+ output.append(line).append('\n');
+ }
+ in.close();
+
+ // Save changes
+ FileWriter out = new FileWriter(file);
+ out.write(output.toString());
+ out.close();
+ }
}
diff --git a/src/zutil/net/http/HttpServer.java b/src/zutil/net/http/HttpServer.java
index fa2945b..2a3a46f 100644
--- a/src/zutil/net/http/HttpServer.java
+++ b/src/zutil/net/http/HttpServer.java
@@ -123,6 +123,8 @@ public class HttpServer extends ThreadedTCPNetworkServer{
* @param page The page itself
*/
public void setPage(String name, HttpPage page){
+ if(name.charAt(0) != '/')
+ name = "/"+name;
pages.put(name, page);
}
@@ -268,9 +270,12 @@ public class HttpServer extends ThreadedTCPNetworkServer{
} catch (Exception e1) {}
if(e.getMessage() != null)
out.println( "500 Internal Server Error: "+e.getMessage() );
- else{
+ else if(e.getCause() != null){
out.println( "500 Internal Server Error: "+e.getCause().getMessage() );
- }
+ }
+ else{
+ out.println( "500 Internal Server Error: "+e);
+ }
}
try{
diff --git a/src/zutil/osal/OSAbstractionLayer.java b/src/zutil/osal/OSAbstractionLayer.java
index b3f8530..b0fa4fe 100644
--- a/src/zutil/osal/OSAbstractionLayer.java
+++ b/src/zutil/osal/OSAbstractionLayer.java
@@ -39,13 +39,13 @@ public abstract class OSAbstractionLayer {
// Variables
private static OSAbstractionLayer instance;
- public OSAbstractionLayer getInstance(){
+ public static OSAbstractionLayer getInstance(){
if(instance == null)
instance = getAbstractionLayer();
return instance;
}
- private OSAbstractionLayer getAbstractionLayer(){
+ private static OSAbstractionLayer getAbstractionLayer(){
String os = System.getProperty("os.name");
if (os.contains("Linux")) return new OsalLinuxImpl();
else if(os.contains("Windows")) return new OsalWindowsImpl();
diff --git a/src/zutil/plugin/PluginData.java b/src/zutil/plugin/PluginData.java
index 4a3e989..c18e400 100644
--- a/src/zutil/plugin/PluginData.java
+++ b/src/zutil/plugin/PluginData.java
@@ -54,7 +54,8 @@ public class PluginData {
@SuppressWarnings("unchecked")
public T getObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
if(obj == null)
- obj = (T) Class.forName(pluginClass).newInstance();
+ new URLClassLoader(urls);
+ //obj = (T) Class.forName(pluginClass).newInstance();
return obj;
}
diff --git a/src/zutil/test/DynamicByteArrayStreamTest.java b/src/zutil/test/DynamicByteArrayStreamTest.java
index fcef5df..1391714 100644
--- a/src/zutil/test/DynamicByteArrayStreamTest.java
+++ b/src/zutil/test/DynamicByteArrayStreamTest.java
@@ -48,6 +48,9 @@ public class DynamicByteArrayStreamTest {
DynamicByteArrayStream out = new DynamicByteArrayStream();
out.append(b);
- assertEquals(b, out.getBytes());
+ byte[] result = out.getBytes();
+ for(int i=0; i