Author: ggregory
Date: Mon Oct 31 18:35:16 2011
New Revision: 1195602
URL: http://svn.apache.org/viewvc?rev=1195602&view=rev
Log:
[VFS-373] Add FileContent write APIs.
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileContent.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileUtil.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderWriteTests.java
commons/proper/vfs/trunk/src/changes/changes.xml
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileContent.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileContent.java?rev=1195602&r1=1195601&r2=1195602&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileContent.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileContent.java
Mon Oct 31 18:35:16 2011
@@ -16,6 +16,7 @@
*/
package org.apache.commons.vfs2;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.cert.Certificate;
@@ -234,4 +235,51 @@ public interface FileContent
* @return true if the file is open, false otherwise.
*/
boolean isOpen();
+
+ /**
+ * Writes this content to another FileContent.
+ *
+ * @param output
+ * The target OutputStream.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ long write(FileContent output) throws IOException;
+
+ /**
+ * Writes this content to another FileObject.
+ *
+ * @param file
+ * The target FileObject.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ long write(FileObject file) throws IOException;
+
+ /**
+ * Writes this content to an OutputStream.
+ *
+ * @param output
+ * The target OutputStream.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ long write(OutputStream output) throws IOException;
+
+ /**
+ * Writes this content to an OutputStream.
+ *
+ * @param output
+ * The target OutputStream.
+ * @param bufferSize
+ * The buffer size to write data chunks.
+ * @throws IOException
+ * if an error occurs writing the file.
+ * @since 2.1
+ */
+ long write(OutputStream output, int bufferSize) throws IOException;
+
}
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileUtil.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileUtil.java?rev=1195602&r1=1195601&r2=1195602&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileUtil.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileUtil.java
Mon Oct 31 18:35:16 2011
@@ -28,9 +28,6 @@ import java.io.OutputStream;
public final class FileUtil
{
- /** The buffer size */
- private static final int BUFFER_SIZE = 1024;
-
private FileUtil()
{
}
@@ -69,31 +66,15 @@ public final class FileUtil
/**
* Writes the content of a file to an OutputStream.
* @param file The FileObject to write.
- * @param outstr The OutputStream to write to.
+ * @param output The OutputStream to write to.
* @throws IOException if an error occurs writing the file.
+ * @see FileContent#write(OutputStream)
*/
public static void writeContent(final FileObject file,
- final OutputStream outstr)
+ final OutputStream output)
throws IOException
{
- final InputStream instr = file.getContent().getInputStream();
- try
- {
- final byte[] buffer = new byte[BUFFER_SIZE];
- while (true)
- {
- final int nread = instr.read(buffer);
- if (nread < 0)
- {
- break;
- }
- outstr.write(buffer, 0, nread);
- }
- }
- finally
- {
- instr.close();
- }
+ file.getContent().write(output);
}
/**
@@ -101,22 +82,14 @@ public final class FileUtil
* @param srcFile The source FileObject.
* @param destFile The target FileObject
* @throws IOException If an error occurs copying the file.
+ * @see FileContent#write(FileContent)
+ * @see FileContent#write(FileObject)
*/
public static void copyContent(final FileObject srcFile,
final FileObject destFile)
throws IOException
{
- // Create the output stream via getContent(), to pick up the
- // validation it does
- final OutputStream outstr = destFile.getContent().getOutputStream();
- try
- {
- writeContent(srcFile, outstr);
- }
- finally
- {
- outstr.close();
- }
+ srcFile.getContent().write(destFile);
}
}
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java?rev=1195602&r1=1195601&r2=1195602&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
Mon Oct 31 18:35:16 2011
@@ -49,6 +49,11 @@ public final class DefaultFileContent im
static final int STATE_RANDOM_ACCESS = 3;
*/
+ /**
+ * The default buffer size for {@link #write(OutputStream)}
+ */
+ private static final int WRITE_BUFFER_SIZE = 4096;
+
static final int STATE_CLOSED = 0;
static final int STATE_OPENED = 1;
@@ -733,4 +738,87 @@ public final class DefaultFileContent im
return fileContentInfo;
}
+
+ /**
+ * Writes this content to another FileContent.
+ *
+ * @param fileContent
+ * The target FileContent.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ public long write(final FileContent fileContent) throws IOException
+ {
+ final OutputStream output = fileContent.getOutputStream();
+ try
+ {
+ return this.write(output);
+ }
+ finally
+ {
+ output.close();
+ }
+ }
+
+ /**
+ * Writes this content to another FileObject.
+ *
+ * @param file
+ * The target FileObject.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ public long write(final FileObject file) throws IOException
+ {
+ return write(file.getContent());
+ }
+
+ /**
+ * Writes this content to an OutputStream.
+ *
+ * @param output
+ * The target OutputStream.
+ * @throws IOException
+ * if an error occurs writing the content.
+ * @since 2.1
+ */
+ public long write(final OutputStream output) throws IOException
+ {
+ return write(output, WRITE_BUFFER_SIZE);
+ }
+
+ /**
+ * Writes this content to an OutputStream.
+ *
+ * @param output
+ * The target OutputStream.
+ * @param bufferSize
+ * The buffer size to write data chunks.
+ * @throws IOException
+ * if an error occurs writing the file.
+ * @since 2.1
+ */
+ public long write(final OutputStream output, final int bufferSize) throws
IOException
+ {
+ final InputStream input = this.getInputStream();
+ long count = 0;
+ try
+ {
+ // This read/write code from Apache Commons IO
+ final byte[] buffer = new byte[bufferSize];
+ int n = 0;
+ while (-1 != (n = input.read(buffer)))
+ {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ }
+ finally
+ {
+ input.close();
+ }
+ return count;
+ }
}
Modified:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderWriteTests.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderWriteTests.java?rev=1195602&r1=1195601&r2=1195602&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderWriteTests.java
(original)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderWriteTests.java
Mon Oct 31 18:35:16 2011
@@ -23,6 +23,7 @@ import java.util.Set;
import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileChangeEvent;
+import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
@@ -361,7 +362,7 @@ public class ProviderWriteTests
/**
* Tests overwriting a file on the same file system.
*/
- public void testOverwriteSameFileSystem() throws Exception
+ public void testCopyFromOverwriteSameFileSystem() throws Exception
{
final FileObject scratchFolder = createScratchFolder();
@@ -645,4 +646,126 @@ public class ProviderWriteTests
assertEquals("Missing event", 0, events.size());
}
}
+
+ /**
+ * Tests file write to and from the same filesystem type
+ */
+ public void testWriteSameFileSystem() throws Exception
+ {
+ final FileObject scratchFolder = createScratchFolder();
+
+ // Create direct child of the test folder
+ final FileObject file = scratchFolder.resolveFile("file1.txt");
+ assertTrue(!file.exists());
+
+ // Create the source file
+ final String content = "Here is some sample content for the file.
Blah Blah Blah.";
+ final OutputStream os = file.getContent().getOutputStream();
+ try
+ {
+ os.write(content.getBytes("utf-8"));
+ }
+ finally
+ {
+ os.close();
+ }
+
+ assertSameContent(content, file);
+
+ // Make sure we can copy the new file to another file on the same
filesystem
+ FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
+ assertTrue(!fileCopy.exists());
+
+ //fileCopy.copyFrom(file, Selectors.SELECT_SELF);
+ //assertSameContent(content, fileCopy);
+ final FileContent sourceContent = file.getContent();
+ //
+ sourceContent.write(fileCopy.getContent());
+ assertSameContent(content, fileCopy);
+ //
+ OutputStream output = sourceContent.getOutputStream();
+ try {
+ sourceContent.write(output);
+ assertSameContent(content, fileCopy);
+ } finally {
+ output.close();
+ }
+ //
+ output = sourceContent.getOutputStream();
+ try {
+ sourceContent.write(output, 1234);
+ assertSameContent(content, fileCopy);
+ } finally {
+ output.close();
+ }
+ }
+
+ /**
+ * Tests overwriting a file on the same file system.
+ */
+ public void testOverwriteSameFileSystem() throws Exception
+ {
+ final FileObject scratchFolder = createScratchFolder();
+
+ // Create direct child of the test folder
+ final FileObject file = scratchFolder.resolveFile("file1.txt");
+ assertTrue(!file.exists());
+
+ // Create the source file
+ final String content = "Here is some sample content for the file.
Blah Blah Blah.";
+ final OutputStream os = file.getContent().getOutputStream();
+ try
+ {
+ os.write(content.getBytes("utf-8"));
+ }
+ finally
+ {
+ os.close();
+ }
+
+ assertSameContent(content, file);
+
+ // Make sure we can copy the new file to another file on the same file
system
+ FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
+ assertTrue(!fileCopy.exists());
+ file.getContent().write(fileCopy);
+
+ assertSameContent(content, fileCopy);
+
+ // Make sure we can copy the same new file to the same target file on
the same file system
+ assertTrue(fileCopy.exists());
+ file.getContent().write(fileCopy);
+
+ assertSameContent(content, fileCopy);
+
+ // Make sure we can copy the same new file to the same target file on
the same file system
+ assertTrue(fileCopy.exists());
+ file.getContent().write(fileCopy.getContent());
+
+ assertSameContent(content, fileCopy);
+
+ // Make sure we can copy the same new file to the same target file on
the same file system
+ assertTrue(fileCopy.exists());
+ OutputStream outputStream = fileCopy.getContent().getOutputStream();
+ try {
+ file.getContent().write(outputStream);
+ }
+ finally {
+ outputStream.close();
+ }
+ assertSameContent(content, fileCopy);
+
+ // Make sure we can copy the same new file to the same target file on
the same file system
+ assertTrue(fileCopy.exists());
+ outputStream = fileCopy.getContent().getOutputStream();
+ try {
+ file.getContent().write(outputStream, 1234);
+ }
+ finally {
+ outputStream.close();
+ }
+ assertSameContent(content, fileCopy);
+}
+
+
}
Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1195602&r1=1195601&r2=1195602&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Mon Oct 31 18:35:16 2011
@@ -23,13 +23,16 @@
<body>
<release version="2.1" date="TBD" description="">
- <action issue="VFS-372" dev="ggregory" type="update">
+ <action issue="VFS-373" dev="ggregory" type="add">
+ Add FileContent write APIs.
+ </action>
+ <action issue="VFS-372" dev="ggregory" type="add">
Add constructors FileDepthSelector() and FileDepthSelector(int).
</action>
- <action issue="VFS-370" dev="ggregory" type="update">
+ <action issue="VFS-370" dev="ggregory" type="add">
Add a FileExtensionSelector class.
</action>
- <action issue="VFS-367" dev="ggregory" type="update">
+ <action issue="VFS-367" dev="ggregory" type="add">
Add APIs FileObject isFile(), FileObject isFolder(), and FileName
isFile().
</action>
<action issue="VFS-366" dev="ggregory" type="update">