Code clean-up, no functional changes.

This commit is contained in:
Harald Kuhr
2011-12-20 15:38:06 +01:00
parent 7435c12a80
commit 6ba32b657a
5 changed files with 133 additions and 164 deletions

View File

@@ -39,7 +39,7 @@ import java.io.ByteArrayInputStream;
* <p/>
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/FastByteArrayOutputStream.java#2 $
* @version $Id: FastByteArrayOutputStream.java#2 $
*/
// TODO: Performance test of a stream impl that uses list of fixed size blocks, rather than contiguous block
public final class FastByteArrayOutputStream extends ByteArrayOutputStream {
@@ -78,23 +78,24 @@ public final class FastByteArrayOutputStream extends ByteArrayOutputStream {
else if (pLength == 0) {
return;
}
int newcount = count + pLength;
growIfNeeded(newcount);
int newCount = count + pLength;
growIfNeeded(newCount);
System.arraycopy(pBytes, pOffset, buf, count, pLength);
count = newcount;
count = newCount;
}
@Override
public synchronized void write(int pByte) {
int newcount = count + 1;
growIfNeeded(newcount);
int newCount = count + 1;
growIfNeeded(newCount);
buf[count] = (byte) pByte;
count = newcount;
count = newCount;
}
private void growIfNeeded(int pNewcount) {
if (pNewcount > buf.length) {
int newSize = Math.max(Math.min(buf.length << 1, buf.length + maxGrowSize), pNewcount);
private void growIfNeeded(int pNewCount) {
if (pNewCount > buf.length) {
int newSize = Math.max(Math.min(buf.length << 1, buf.length + maxGrowSize), pNewCount);
byte newBuf[] = new byte[newSize];
System.arraycopy(buf, 0, newBuf, 0, count);
buf = newBuf;
@@ -110,9 +111,10 @@ public final class FastByteArrayOutputStream extends ByteArrayOutputStream {
// Non-synchronized version of toByteArray
@Override
public byte[] toByteArray() {
byte newbuf[] = new byte[count];
System.arraycopy(buf, 0, newbuf, 0, count);
return newbuf;
byte newBuf[] = new byte[count];
System.arraycopy(buf, 0, newBuf, 0, count);
return newBuf;
}
/**

View File

@@ -38,7 +38,7 @@ import java.io.InputStreamReader;
* <p/>
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/FileSystem.java#1 $
* @version $Id: FileSystem.java#1 $
*/
abstract class FileSystem {
abstract long getFreeSpace(File pPath);
@@ -57,21 +57,21 @@ abstract class FileSystem {
//System.out.println("os = " + os);
os = os.toLowerCase();
if (os.indexOf("windows") != -1) {
if (os.contains("windows")) {
return new Win32FileSystem();
}
else if (os.indexOf("linux") != -1 ||
os.indexOf("sun os") != -1 ||
os.indexOf("sunos") != -1 ||
os.indexOf("solaris") != -1 ||
os.indexOf("mpe/ix") != -1 ||
os.indexOf("hp-ux") != -1 ||
os.indexOf("aix") != -1 ||
os.indexOf("freebsd") != -1 ||
os.indexOf("irix") != -1 ||
os.indexOf("digital unix") != -1 ||
os.indexOf("unix") != -1 ||
os.indexOf("mac os x") != -1) {
else if (os.contains("linux") ||
os.contains("sun os") ||
os.contains("sunos") ||
os.contains("solaris") ||
os.contains("mpe/ix") ||
os.contains("hp-ux") ||
os.contains("aix") ||
os.contains("freebsd") ||
os.contains("irix") ||
os.contains("digital unix") ||
os.contains("unix") ||
os.contains("mac os x")) {
return new UnixFileSystem();
}
else {

View File

@@ -186,6 +186,7 @@ public final class FileUtil {
if (!pOverWrite && pToFile.exists()) {
return false;
}
InputStream in = null;
OutputStream out = null;
@@ -202,6 +203,7 @@ public final class FileUtil {
close(in);
close(out);
}
return true; // If we got here, everything's probably okay.. ;-)
}
@@ -307,6 +309,8 @@ public final class FileUtil {
Validate.notNull(pFrom, "from");
Validate.notNull(pTo, "to");
// TODO: Consider using file channels for faster copy where possible
// Use buffer size two times byte array, to avoid i/o bottleneck
// TODO: Consider letting the client decide as this is sometimes not a good thing!
InputStream in = new BufferedInputStream(pFrom, BUF_SIZE * 2);
@@ -322,31 +326,9 @@ public final class FileUtil {
// Flush out stream, to write any remaining buffered data
out.flush();
return true; // If we got here, everything's probably okay.. ;-)
return true; // If we got here, everything is probably okay.. ;-)
}
/*
// Consider using the example from
// http://developer.java.sun.com/developer/Books/performance/ch04.pdf
// Test if this is really faster. And what about a lot of concurrence?
// Have a pool of buffers? :-)
static final int BUFF_SIZE = 100000;
static final byte[] buffer = new byte[BUFF_SIZE];
public static void copy(InputStream in, OutputStream out) throws IOException {
while (true) {
synchronized (buffer) {
int amountRead = in.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
}
*/
/**
* Gets the file (type) extension of the given file.
* A file extension is the part of the filename, after the last occurence