This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push:
new ec5eb96 Add ability to specify buffer sizes (#59)
ec5eb96 is described below
commit ec5eb9601d4d9175ffb242cb5647ce4abcf1668f
Author: Boris Petrov <[email protected]>
AuthorDate: Fri Apr 26 00:22:08 2019 +0300
Add ability to specify buffer sizes (#59)
* Code style
* Add ability to specify a buffer size for the input stream
* Add ability to specify a buffer size for the output stream
---
.../java/org/apache/commons/vfs2/FileContent.java | 68 ++++++++++++
.../commons/vfs2/provider/DefaultFileContent.java | 120 ++++++++++++++++-----
.../commons/vfs2/util/MonitorInputStream.java | 18 +++-
.../commons/vfs2/util/MonitorOutputStream.java | 16 +++
.../vfs2/provider/DefaultFileContentTest.java | 30 ++++++
5 files changed, 222 insertions(+), 30 deletions(-)
diff --git
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
index 39ae7c2..0500d22 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
@@ -139,6 +139,7 @@ public interface FileContent extends Closeable {
* Returns an input stream for reading the file's content.
* <p>
* There may only be a single input or output stream open for the file at
any time.
+ * </p>
*
* @return An input stream to read the file's content from. The input
stream is buffered, so there is no need to
* wrap it in a {@code BufferedInputStream}.
@@ -148,12 +149,31 @@ public interface FileContent extends Closeable {
InputStream getInputStream() throws FileSystemException;
/**
+ * Returns an input stream for reading the file's content.
+ * <p>
+ * There may only be a single input or output stream open for the file at
any time.
+ * </p>
+ *
+ * @param bufferSize The buffer size to use.
+ * @return An input stream to read the file's content from. The input
stream is buffered, so there is no need to
+ * wrap it in a {@code BufferedInputStream}.
+ * @throws FileSystemException If the file does not exist, or is being
read, or is being written, or on error
+ * opening the stream.
+ * @since 2.4
+ */
+ default InputStream getInputStream(int bufferSize) throws
FileSystemException {
+ return getInputStream();
+ }
+
+ /**
* Returns an output stream for writing the file's content.
* <p>
* If the file does not exist, this method creates it, and the parent
folder, if necessary. If the file does exist,
* it is replaced with whatever is written to the output stream.
+ * </p>
* <p>
* There may only be a single input or output stream open for the file at
any time.
+ * </p>
*
* @return An output stream to write the file's content to. The stream is
buffered, so there is no need to wrap it
* in a {@code BufferedOutputStream}.
@@ -168,8 +188,10 @@ public interface FileContent extends Closeable {
* If the file does not exist, and you use one of the write* methods, this
method creates it, and the parent folder,
* if necessary. If the file does exist, parts of the file are replaced
with whatever is written at a given
* position.
+ * </p>
* <p>
* There may only be a single input or output stream open for the file at
any time.
+ * </p>
*
* @param mode The mode to use to access the file.
* @return the stream for reading and writing the file's content.
@@ -183,8 +205,10 @@ public interface FileContent extends Closeable {
* <p>
* If the file does not exist, this method creates it, and the parent
folder, if necessary. If the file does exist,
* it is replaced with whatever is written to the output stream.
+ * </p>
* <p>
* There may only be a single input or output stream open for the file at
any time.
+ * </p>
*
* @param bAppend true if you would like to append to the file. This may
not be supported by all implementations.
* @return An output stream to write the file's content to. The stream is
buffered, so there is no need to wrap it
@@ -195,10 +219,54 @@ public interface FileContent extends Closeable {
OutputStream getOutputStream(boolean bAppend) throws FileSystemException;
/**
+ * Returns an output stream for writing the file's content.
+ * <p>
+ * If the file does not exist, this method creates it, and the parent
folder, if necessary. If the file does exist,
+ * it is replaced with whatever is written to the output stream.
+ * </p>
+ * <p>
+ * There may only be a single input or output stream open for the file at
any time.
+ * </p>
+ *
+ * @param bufferSize The buffer size to use.
+ * @return An output stream to write the file's content to. The stream is
buffered, so there is no need to wrap it
+ * in a {@code BufferedOutputStream}.
+ * @throws FileSystemException If the file is read-only, or is being read,
or is being written, or bAppend is true
+ * and the implementation does not support it, or on error
opening the stream.
+ * @since 2.4
+ */
+ default OutputStream getOutputStream(int bufferSize) throws
FileSystemException {
+ return getOutputStream();
+ }
+
+ /**
+ * Returns an output stream for writing the file's content.
+ * <p>
+ * If the file does not exist, this method creates it, and the parent
folder, if necessary. If the file does exist,
+ * it is replaced with whatever is written to the output stream.
+ * </p>
+ * <p>
+ * There may only be a single input or output stream open for the file at
any time.
+ * </p>
+ *
+ * @param bAppend true if you would like to append to the file. This may
not be supported by all implementations.
+ * @param bufferSize The buffer size to use.
+ * @return An output stream to write the file's content to. The stream is
buffered, so there is no need to wrap it
+ * in a {@code BufferedOutputStream}.
+ * @throws FileSystemException If the file is read-only, or is being read,
or is being written, or bAppend is true
+ * and the implementation does not support it, or on error
opening the stream.
+ * @since 2.4
+ */
+ default OutputStream getOutputStream(boolean bAppend, int bufferSize)
throws FileSystemException {
+ return getOutputStream(bAppend);
+ }
+
+ /**
* Closes all resources used by the content, including any open stream.
Commits pending changes to the file.
* <p>
* This method is a hint to the implementation that it can release
resources. This object can continue to be used
* after calling this method.
+ * </p>
*
* @throws FileSystemException if an error occurs closing the file.
*/
diff --git
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
index 619268b..1a6da74 100644
---
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
+++
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
@@ -340,20 +340,20 @@ public final class DefaultFileContent implements
FileContent {
*/
@Override
public InputStream getInputStream() throws FileSystemException {
- /*
- * if (getThreadData().getState() == STATE_WRITING ||
getThreadData().getState() == STATE_RANDOM_ACCESS) { throw
- * new FileSystemException("vfs.provider/read-in-use.error", file); }
- */
-
- // Get the raw input stream
- final InputStream inputStream = fileObject.getInputStream();
-
- final InputStream wrappedInputStream = new
FileContentInputStream(fileObject, inputStream);
-
- getOrCreateThreadData().addInstr(wrappedInputStream);
- streamOpened();
+ return buildInputStream(null);
+ }
- return wrappedInputStream;
+ /**
+ * Returns an input stream for reading the content.
+ *
+ * @param bufferSize The buffer size to use.
+ * @return The InputStream
+ * @throws FileSystemException if an error occurs.
+ * @since 2.4
+ */
+ @Override
+ public InputStream getInputStream(final int bufferSize) throws
FileSystemException {
+ return buildInputStream(bufferSize);
}
/**
@@ -401,24 +401,34 @@ public final class DefaultFileContent implements
FileContent {
*/
@Override
public OutputStream getOutputStream(final boolean bAppend) throws
FileSystemException {
- /*
- * if (getThreadData().getState() != STATE_NONE)
- */
- final FileContentThreadData streams = getOrCreateThreadData();
-
- if (streams.getOutstr() != null) {
- throw new FileSystemException("vfs.provider/write-in-use.error",
fileObject);
- }
-
- // Get the raw output stream
- final OutputStream outstr = fileObject.getOutputStream(bAppend);
+ return buildOutputStream(bAppend, null);
+ }
- // Create and set wrapper
- final FileContentOutputStream wrapped = new
FileContentOutputStream(fileObject, outstr);
- streams.setOutstr(wrapped);
- streamOpened();
+ /**
+ * Returns an output stream for writing the content.
+ *
+ * @param bufferSize The buffer size to use.
+ * @return The OutputStream for the file.
+ * @throws FileSystemException if an error occurs.
+ * @since 2.4
+ */
+ @Override
+ public OutputStream getOutputStream(final int bufferSize) throws
FileSystemException {
+ return buildOutputStream(false, bufferSize);
+ }
- return wrapped;
+ /**
+ * Returns an output stream for writing the content in append mode.
+ *
+ * @param bAppend true if the data written should be appended.
+ * @param bufferSize The buffer size to use.
+ * @return The OutputStream for the file.
+ * @throws FileSystemException if an error occurs.
+ * @since 2.4
+ */
+ @Override
+ public OutputStream getOutputStream(final boolean bAppend, final int
bufferSize) throws FileSystemException {
+ return buildOutputStream(bAppend, bufferSize);
}
/**
@@ -475,6 +485,48 @@ public final class DefaultFileContent implements
FileContent {
}
}
+ private InputStream buildInputStream(final Integer bufferSize) throws
FileSystemException {
+ /*
+ * if (getThreadData().getState() == STATE_WRITING ||
getThreadData().getState() == STATE_RANDOM_ACCESS) { throw
+ * new FileSystemException("vfs.provider/read-in-use.error", file); }
+ */
+
+ // Get the raw input stream
+ final InputStream inputStream = fileObject.getInputStream();
+
+ final InputStream wrappedInputStream = bufferSize == null ?
+ new FileContentInputStream(fileObject, inputStream) :
+ new FileContentInputStream(fileObject, inputStream, bufferSize);
+
+ getOrCreateThreadData().addInstr(wrappedInputStream);
+ streamOpened();
+
+ return wrappedInputStream;
+ }
+
+ private OutputStream buildOutputStream(final boolean bAppend, final
Integer bufferSize) throws FileSystemException {
+ /*
+ * if (getThreadData().getState() != STATE_NONE)
+ */
+ final FileContentThreadData streams = getOrCreateThreadData();
+
+ if (streams.getOutstr() != null) {
+ throw new FileSystemException("vfs.provider/write-in-use.error",
fileObject);
+ }
+
+ // Get the raw output stream
+ final OutputStream outstr = fileObject.getOutputStream(bAppend);
+
+ // Create and set wrapper
+ final FileContentOutputStream wrapped = bufferSize == null ?
+ new FileContentOutputStream(fileObject, outstr) :
+ new FileContentOutputStream(fileObject, outstr, bufferSize);
+ streams.setOutstr(wrapped);
+ streamOpened();
+
+ return wrapped;
+ }
+
/**
* Handles the end of input stream.
*/
@@ -562,6 +614,11 @@ public final class DefaultFileContent implements
FileContent {
this.file = file;
}
+ FileContentInputStream(final FileObject file, final InputStream instr,
final int bufferSize) {
+ super(instr, bufferSize);
+ this.file = file;
+ }
+
/**
* Closes this input stream.
*/
@@ -633,6 +690,11 @@ public final class DefaultFileContent implements
FileContent {
this.file = file;
}
+ FileContentOutputStream(final FileObject file, final OutputStream
outstr, final int bufferSize) {
+ super(outstr, bufferSize);
+ this.file = file;
+ }
+
/**
* Closes this output stream.
*/
diff --git
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java
index 97bfca5..a7bd8fd 100644
---
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java
+++
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorInputStream.java
@@ -30,11 +30,27 @@ public class MonitorInputStream extends BufferedInputStream
{
private final AtomicBoolean finished = new AtomicBoolean(false);
private final AtomicLong atomicCount = new AtomicLong(0);
+ /**
+ * Constructs a MonitorInputStream from the passed InputStream
+ *
+ * @param in The input stream to wrap.
+ */
public MonitorInputStream(final InputStream in) {
super(in);
}
/**
+ * Constructs a MonitorInputStream from the passed InputStream and with
the specified buffer size
+ *
+ * @param in The input stream to wrap.
+ * @param bufferSize The buffer size to use.
+ * @since 2.4
+ */
+ public MonitorInputStream(final InputStream in, final int bufferSize) {
+ super(in, bufferSize);
+ }
+
+ /**
* Returns 0 if the stream is at EOF, else the underlying inputStream will
be queried.
*
* @return The number of bytes that are available.
@@ -90,7 +106,7 @@ public class MonitorInputStream extends BufferedInputStream {
final int nread = super.read(buffer, offset, length);
if (nread != EOF_CHAR) {
- atomicCount.addAndGet(nread);
+ atomicCount.addAndGet(nread);
return nread;
}
return EOF_CHAR;
diff --git
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
index f473b81..3e98194 100644
---
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
+++
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
@@ -29,11 +29,27 @@ import org.apache.commons.vfs2.FileSystemException;
public class MonitorOutputStream extends BufferedOutputStream {
private final AtomicBoolean finished = new AtomicBoolean(false);
+ /**
+ * Constructs a MonitorOutputStream from the passed OutputStream
+ *
+ * @param out The output stream to wrap.
+ */
public MonitorOutputStream(final OutputStream out) {
super(out);
}
/**
+ * Constructs a MonitorOutputStream from the passed OutputStream and with
the specified buffer size
+ *
+ * @param out The output stream to wrap.
+ * @param bufferSize The buffer size to use.
+ * @since 2.4
+ */
+ public MonitorOutputStream(final OutputStream out, final int bufferSize) {
+ super(out, bufferSize);
+ }
+
+ /**
* Closes this output stream.
* <p>
* This makes sure the buffers are flushed, close the output stream and it
will call {@link #onClose()} and re-throw
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
index 2c13b8f..084d769 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
@@ -84,4 +84,34 @@ public class DefaultFileContentTest {
}
}
}
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testPassingIllegalBufferSizeToInputStream() throws Exception {
+ final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final FileSystemManager fileSystemManager = VFS.getManager();
+
+ try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ file.getContent().getInputStream(-2);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testPassingIllegalBufferSizeToOutputStream() throws Exception {
+ final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final FileSystemManager fileSystemManager = VFS.getManager();
+
+ try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ file.getContent().getOutputStream(0);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testPassingIllegalBufferSizeToOutputStreamWithAppendFlag()
throws Exception {
+ final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final FileSystemManager fileSystemManager = VFS.getManager();
+
+ try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ file.getContent().getOutputStream(true, -1);
+ }
+ }
}