You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2009/04/08 13:17:18 UTC

svn commit: r763187 - in /wicket/trunk/wicket/src/main/java/org/apache/wicket: markup/html/form/upload/FileUpload.java util/file/Files.java util/io/Streams.java

Author: jdonnerstag
Date: Wed Apr  8 11:17:18 2009
New Revision: 763187

URL: http://svn.apache.org/viewvc?rev=763187&view=rev
Log:
fixed WICKET-2076 Need a way to programmaticaly configure the size of the chunk buffer
Issue: WICKET-2076

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/file/Files.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/io/Streams.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java?rev=763187&r1=763186&r2=763187&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java Wed Apr  8 11:17:18 2009
@@ -161,10 +161,24 @@
 	 */
 	public void writeTo(final File file) throws IOException
 	{
+		writeTo(file, 4096);
+	}
+
+	/**
+	 * Saves this file upload to a given file on the server side.
+	 * 
+	 * @param file
+	 *            The file
+	 * @param bufSize
+	 *            The memory buffer size
+	 * @throws IOException
+	 */
+	public void writeTo(final File file, final int bufSize) throws IOException
+	{
 		InputStream is = getInputStream();
 		try
 		{
-			Files.writeTo(file, is);
+			Files.writeTo(file, is, bufSize);
 		}
 		finally
 		{

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/file/Files.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/file/Files.java?rev=763187&r1=763186&r2=763187&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/file/Files.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/file/Files.java Wed Apr  8 11:17:18 2009
@@ -122,10 +122,28 @@
 	public static final int writeTo(final java.io.File file, final InputStream input)
 		throws IOException
 	{
+		return writeTo(file, input, 4096);
+	}
+
+	/**
+	 * Writes the given input stream to the given file
+	 * 
+	 * @param file
+	 *            The file to write to
+	 * @param input
+	 *            The input
+	 * @param bufSize
+	 *            The memory buffer size. 4096 is a good value.
+	 * @return Number of bytes written
+	 * @throws IOException
+	 */
+	public static final int writeTo(final java.io.File file, final InputStream input,
+		final int bufSize) throws IOException
+	{
 		final FileOutputStream out = new FileOutputStream(file);
 		try
 		{
-			return Streams.copy(input, out);
+			return Streams.copy(input, out, bufSize);
 		}
 		finally
 		{

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/io/Streams.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/io/Streams.java?rev=763187&r1=763186&r2=763187&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/io/Streams.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/io/Streams.java Wed Apr  8 11:17:18 2009
@@ -48,10 +48,11 @@
 public final class Streams
 {
 	private static final String XML_PROPERTIES_DTD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-			+ "<!-- DTD for properties -->" + "<!ELEMENT properties ( comment?, entry* ) >"
-			+ "<!ATTLIST properties" + " version CDATA #FIXED \"1.0\">"
-			+ "<!ELEMENT comment (#PCDATA) >" + "<!ELEMENT entry (#PCDATA) >" + "<!ATTLIST entry "
-			+ " key CDATA #REQUIRED>";
+		+ "<!-- DTD for properties -->" + "<!ELEMENT properties ( comment?, entry* ) >"
+		+ "<!ATTLIST properties" + " version CDATA #FIXED \"1.0\">"
+		+ "<!ELEMENT comment (#PCDATA) >" + "<!ELEMENT entry (#PCDATA) >" + "<!ATTLIST entry "
+		+ " key CDATA #REQUIRED>";
+
 
 	/**
 	 * Writes the input stream to the output stream. Input is done without a Reader object, meaning
@@ -66,7 +67,31 @@
 	 */
 	public static int copy(final InputStream in, final OutputStream out) throws IOException
 	{
-		final byte[] buffer = new byte[4096];
+		return copy(in, out, 4096);
+	}
+
+	/**
+	 * Writes the input stream to the output stream. Input is done without a Reader object, meaning
+	 * that the input is copied in its raw form.
+	 * 
+	 * @param in
+	 *            The input stream
+	 * @param out
+	 *            The output stream
+	 * @param bufSize
+	 *            The buffer size. A good value is 4096.
+	 * @return Number of bytes copied from one stream to the other
+	 * @throws IOException
+	 */
+	public static int copy(final InputStream in, final OutputStream out, final int bufSize)
+		throws IOException
+	{
+		if (bufSize <= 0)
+		{
+			throw new IllegalArgumentException("The parameter 'bufSize' must not be <= 0");
+		}
+
+		final byte[] buffer = new byte[bufSize];
 		int bytesCopied = 0;
 		while (true)
 		{
@@ -91,7 +116,7 @@
 	 *             When the input stream could not be read from
 	 */
 	public static void loadFromXml(Properties properties, InputStream inputStream)
-			throws IOException
+		throws IOException
 	{
 		if (properties == null)
 		{
@@ -117,7 +142,7 @@
 			db.setEntityResolver(new EntityResolver()
 			{
 				public InputSource resolveEntity(String publicId, String systemId)
-						throws SAXException
+					throws SAXException
 				{
 					if (systemId.equals("http://java.sun.com/dtd/properties.dtd"))
 					{
@@ -198,7 +223,7 @@
 	 * @throws IOException
 	 */
 	public static String readString(final InputStream in, final CharSequence encoding)
-			throws IOException
+		throws IOException
 	{
 		return readString(new BufferedReader(new InputStreamReader(in, encoding.toString())));
 	}