Skip to content

Commit

Permalink
Sorted members.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/incubator/wicket/trunk@458650 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Jonathan Locke committed Jan 14, 2006
1 parent e57926f commit cf29c25
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 179 deletions.
88 changes: 44 additions & 44 deletions wicket/src/java/wicket/util/file/Files.java
Expand Up @@ -31,29 +31,6 @@
*/
public class Files
{
/**
* Private constructor to prevent instantiation.
*/
private Files()
{
}

/**
* Gets extension from path
*
* @param path
* The path
* @return The extension, like "bmp" or "html", or null if none can be found
*/
public static String extension(final String path)
{
if (path.indexOf('.') != -1)
{
return Strings.lastPathComponent(path, '.');
}
return null;
}

/**
* Strips off the given extension (probably returned from Files.extension())
* from the path, yielding a base pathname.
Expand All @@ -74,38 +51,31 @@ public static String basePath(final String path, final String extension)
}

/**
* Gets filename from path
* Gets extension from path
*
* @param path
* The path
* @return The filename
* @return The extension, like "bmp" or "html", or null if none can be found
*/
public static String filename(final String path)
public static String extension(final String path)
{
return Strings.lastPathComponent(path.replace('/', java.io.File.separatorChar), java.io.File.separatorChar);
if (path.indexOf('.') != -1)
{
return Strings.lastPathComponent(path, '.');
}
return null;
}

/**
* Writes the given input stream to the given file
* Gets filename from path
*
* @param file
* The file to write to
* @param input
* The input
* @throws IOException
* @param path
* The path
* @return The filename
*/
public static final void writeTo(final java.io.File file, final InputStream input)
throws IOException
public static String filename(final String path)
{
FileOutputStream fos = new FileOutputStream(file);
try
{
Streams.copy(input, fos);
}
finally
{
fos.close();
}
return Strings.lastPathComponent(path.replace('/', java.io.File.separatorChar), java.io.File.separatorChar);
}

/**
Expand Down Expand Up @@ -136,4 +106,34 @@ public static boolean remove(final java.io.File file)
}
return true;
}

/**
* Writes the given input stream to the given file
*
* @param file
* The file to write to
* @param input
* The input
* @throws IOException
*/
public static final void writeTo(final java.io.File file, final InputStream input)
throws IOException
{
FileOutputStream out = new FileOutputStream(file);
try
{
Streams.copy(input, out);
}
finally
{
out.close();
}
}

/**
* Private constructor to prevent instantiation.
*/
private Files()
{
}
}
189 changes: 94 additions & 95 deletions wicket/src/java/wicket/util/io/ByteArrayOutputStream.java
Expand Up @@ -43,12 +43,11 @@
*/
public class ByteArrayOutputStream extends OutputStream
{

private List buffers = new java.util.ArrayList();
private int count;
private byte[] currentBuffer;
private int currentBufferIndex;
private int filledBufferSum;
private byte[] currentBuffer;
private int count;

/**
* Creates a new byte array output stream. The buffer capacity is initially 1024
Expand All @@ -74,40 +73,78 @@ public ByteArrayOutputStream(int size)
needNewBuffer(size);
}

private byte[] getBuffer(int index)
/**
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in this class
* can be called after the stream has been closed without generating an
* <tt>IOException</tt>.
* @throws IOException in case an I/O error occurs
*/
public void close() throws IOException
{
return (byte[])buffers.get(index);
// nop
}

private void needNewBuffer(int newcount)
/**
* @see java.io.ByteArrayOutputStream#reset()
*/
public synchronized void reset()
{
if (currentBufferIndex < buffers.size() - 1)
{
// Recycling old buffer
filledBufferSum += currentBuffer.length;
count = 0;
filledBufferSum = 0;
currentBufferIndex = 0;
currentBuffer = getBuffer(currentBufferIndex);
}

currentBufferIndex++;
currentBuffer = getBuffer(currentBufferIndex);
}
else
/**
* Gets the size.
* @return the size
*/
public int size()
{
return count;
}

/**
* Writes to a byte array.
* @return this is a byte array
*/
public synchronized byte[] toByteArray()
{
int remaining = count;
int pos = 0;
byte newbuf[] = new byte[count];
for (int i = 0; i < buffers.size(); i++)
{
// Creating new buffer
int newBufferSize;
if (currentBuffer == null)
{
newBufferSize = newcount;
filledBufferSum = 0;
}
else
byte[] buf = getBuffer(i);
int c = Math.min(buf.length, remaining);
System.arraycopy(buf, 0, newbuf, pos, c);
pos += c;
remaining -= c;
if (remaining == 0)
{
newBufferSize = Math.max(currentBuffer.length << 1, newcount - filledBufferSum);
filledBufferSum += currentBuffer.length;
break;
}

currentBufferIndex++;
currentBuffer = new byte[newBufferSize];
buffers.add(currentBuffer);
}
return newbuf;
}

/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return new String(toByteArray());
}

/**
* This as a string using the provided encoding.
* @param enc the encoding to use
* @return This as a string using the provided encoding
* @throws UnsupportedEncodingException
*/
public String toString(String enc) throws UnsupportedEncodingException
{
return new String(toByteArray(), enc);
}

/**
Expand Down Expand Up @@ -150,37 +187,6 @@ public synchronized void write(int b)
write(new byte[] { (byte)b }, 0, 1);
}

/**
* Gets the size.
* @return the size
*/
public int size()
{
return count;
}

/**
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in this class
* can be called after the stream has been closed without generating an
* <tt>IOException</tt>.
* @throws IOException in case an I/O error occurs
*/
public void close() throws IOException
{
// nop
}

/**
* @see java.io.ByteArrayOutputStream#reset()
*/
public synchronized void reset()
{
count = 0;
filledBufferSum = 0;
currentBufferIndex = 0;
currentBuffer = getBuffer(currentBufferIndex);
}

/**
* Write to the given output stream.
* @param out the output stream to write to
Expand All @@ -203,47 +209,40 @@ public synchronized void writeTo(OutputStream out) throws IOException
}
}

/**
* Writes to a byte array.
* @return this is a byte array
*/
public synchronized byte[] toByteArray()
private byte[] getBuffer(int index)
{
int remaining = count;
int pos = 0;
byte newbuf[] = new byte[count];
for (int i = 0; i < buffers.size(); i++)
{
byte[] buf = getBuffer(i);
int c = Math.min(buf.length, remaining);
System.arraycopy(buf, 0, newbuf, pos, c);
pos += c;
remaining -= c;
if (remaining == 0)
{
break;
}
}
return newbuf;
return (byte[])buffers.get(index);
}

/**
* @see java.lang.Object#toString()
*/
public String toString()
private void needNewBuffer(int newcount)
{
return new String(toByteArray());
}
if (currentBufferIndex < buffers.size() - 1)
{
// Recycling old buffer
filledBufferSum += currentBuffer.length;

/**
* This as a string using the provided encoding.
* @param enc the encoding to use
* @return This as a string using the provided encoding
* @throws UnsupportedEncodingException
*/
public String toString(String enc) throws UnsupportedEncodingException
{
return new String(toByteArray(), enc);
currentBufferIndex++;
currentBuffer = getBuffer(currentBufferIndex);
}
else
{
// Creating new buffer
int newBufferSize;
if (currentBuffer == null)
{
newBufferSize = newcount;
filledBufferSum = 0;
}
else
{
newBufferSize = Math.max(currentBuffer.length << 1, newcount - filledBufferSum);
filledBufferSum += currentBuffer.length;
}

currentBufferIndex++;
currentBuffer = new byte[newBufferSize];
buffers.add(currentBuffer);
}
}

}

0 comments on commit cf29c25

Please sign in to comment.