Implement EncFSUtil.copyWholeStream()
Noticed we're doing full InputStream->OutputStream copies quite a bit around the code, so this commit adds a utility function for that and calls it from everywhere.
This commit is contained in:
parent
024b19dc2e
commit
e1027c3b87
5 changed files with 67 additions and 126 deletions
src/main/java
|
@ -30,6 +30,7 @@ import org.mrpdaemon.sec.encfs.EncFSFileInputStream;
|
|||
import org.mrpdaemon.sec.encfs.EncFSInvalidConfigException;
|
||||
import org.mrpdaemon.sec.encfs.EncFSInvalidPasswordException;
|
||||
import org.mrpdaemon.sec.encfs.EncFSUnsupportedException;
|
||||
import org.mrpdaemon.sec.encfs.EncFSUtil;
|
||||
import org.mrpdaemon.sec.encfs.EncFSVolume;
|
||||
|
||||
public class EncFSShell {
|
||||
|
@ -370,14 +371,9 @@ public class EncFSShell {
|
|||
continue;
|
||||
}
|
||||
|
||||
EncFSFileInputStream efis = new EncFSFileInputStream(
|
||||
file);
|
||||
int bytesRead = 0;
|
||||
while (bytesRead >= 0) {
|
||||
byte[] readBuf = new byte[128];
|
||||
bytesRead = efis.read(readBuf);
|
||||
System.out.print(new String(readBuf));
|
||||
}
|
||||
EncFSUtil.copyWholeStream(
|
||||
new EncFSFileInputStream(file), System.out,
|
||||
true, false);
|
||||
System.out.println();
|
||||
} else {
|
||||
System.out.println("File not found!");
|
||||
|
|
|
@ -366,14 +366,17 @@ public class EncFSFile {
|
|||
}
|
||||
return result;
|
||||
} else {
|
||||
if (this.isDirectory() || dstPath.isDirectory()) {
|
||||
throw new IOException("Can't copy directories");
|
||||
}
|
||||
|
||||
try {
|
||||
copyViaStreams(this, dstPath);
|
||||
EncFSUtil.copyWholeStream(this.openInputStream(),
|
||||
dstPath.openOutputStream(), true, true);
|
||||
} catch (EncFSCorruptDataException e) {
|
||||
throw new IOException(e);
|
||||
} catch (EncFSUnsupportedException e) {
|
||||
throw new IOException(e);
|
||||
} catch (EncFSChecksumException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -384,34 +387,4 @@ public class EncFSFile {
|
|||
}
|
||||
}
|
||||
|
||||
// Copy a source file into a target file using decryption/encryption
|
||||
private static void copyViaStreams(EncFSFile srcEncFSFile,
|
||||
EncFSFile targetEncFSFile) throws IOException,
|
||||
EncFSCorruptDataException, EncFSUnsupportedException,
|
||||
EncFSChecksumException {
|
||||
|
||||
if (srcEncFSFile.isDirectory() || targetEncFSFile.isDirectory()) {
|
||||
throw new IllegalArgumentException("Can't copy directories");
|
||||
}
|
||||
|
||||
OutputStream efos = targetEncFSFile.openOutputStream();
|
||||
try {
|
||||
InputStream efis = srcEncFSFile.openInputStream();
|
||||
try {
|
||||
int bytesRead = 0;
|
||||
while (bytesRead >= 0) {
|
||||
byte[] readBuf = new byte[128];
|
||||
bytesRead = efis.read(readBuf);
|
||||
if (bytesRead >= 0) {
|
||||
efos.write(readBuf, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
efis.close();
|
||||
}
|
||||
|
||||
} finally {
|
||||
efos.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,6 +15,10 @@
|
|||
|
||||
package org.mrpdaemon.sec.encfs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Utility class with static methods to help with various functionality
|
||||
*/
|
||||
|
@ -81,4 +85,44 @@ public class EncFSUtil {
|
|||
(byte) ((l >> 0) & 0xff), };
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the entire content of an InputStream into an OutputStream
|
||||
*
|
||||
*
|
||||
* @param in
|
||||
* The InputStream to read data from
|
||||
* @param out
|
||||
* The OutputStream to write data to
|
||||
* @param closeInput
|
||||
* Whether to close the InputStream after the operation
|
||||
* @param closeOutput
|
||||
* Whether to close the OutputStream after the operation
|
||||
*
|
||||
* @throws IOException
|
||||
* I/O exception from read or write
|
||||
*/
|
||||
public static void copyWholeStream(InputStream in, OutputStream out,
|
||||
boolean closeInput, boolean closeOutput) throws IOException {
|
||||
byte[] buf = new byte[8192];
|
||||
int bytesRead = 0;
|
||||
|
||||
try {
|
||||
try {
|
||||
bytesRead = in.read(buf);
|
||||
while (bytesRead >= 0) {
|
||||
out.write(buf, 0, bytesRead);
|
||||
bytesRead = in.read(buf);
|
||||
}
|
||||
} finally {
|
||||
if (closeInput) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (closeOutput) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue