Clean up Improved(Input|Output)Stream

Two main changes:

- Replaced getClass().getSimpleName() in the logs with a constructor-given name. Right now what we have is a lot of identical classes with slightly different names so that the logs would be different. With this change - we can later get rid of a lot of these classes and replace them with simple wrappers.

- Removed the "expected" feature. Only Tar uses it - and it can override onClose to do that (that's what it's there for!)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202129563
This commit is contained in:
guyben 2018-06-26 08:05:47 -07:00 committed by Ben McIlwain
parent 07aead3ca4
commit 3550045636
8 changed files with 34 additions and 37 deletions

View file

@ -246,7 +246,7 @@ public final class Ghostryde {
private final DateTime modified;
Input(@WillCloseWhenClosed InputStream input, String name, DateTime modified) {
super(input);
super("Input", input);
this.name = checkNotNull(name, "name");
this.modified = checkNotNull(modified, "modified");
}
@ -273,7 +273,7 @@ public final class Ghostryde {
@NotThreadSafe
public static final class Output extends ImprovedOutputStream {
Output(@WillCloseWhenClosed OutputStream os) {
super(os);
super("Output", os);
}
}
@ -287,7 +287,7 @@ public final class Ghostryde {
@NotThreadSafe
public static final class Encryptor extends ImprovedOutputStream {
Encryptor(@WillCloseWhenClosed OutputStream os) {
super(os);
super("Encryptor", os);
}
}
@ -303,7 +303,7 @@ public final class Ghostryde {
private final PGPPublicKeyEncryptedData crypt;
Decryptor(@WillCloseWhenClosed InputStream input, PGPPublicKeyEncryptedData crypt) {
super(input);
super("Decryptor", input);
this.crypt = checkNotNull(crypt, "crypt");
}
@ -340,7 +340,7 @@ public final class Ghostryde {
@NotThreadSafe
public static final class Compressor extends ImprovedOutputStream {
Compressor(@WillCloseWhenClosed OutputStream os) {
super(os);
super("Compressor", os);
}
}
@ -354,7 +354,7 @@ public final class Ghostryde {
@NotThreadSafe
public static final class Decompressor extends ImprovedInputStream {
Decompressor(@WillCloseWhenClosed InputStream input) {
super(input);
super("Decompressor", input);
}
}

View file

@ -43,7 +43,7 @@ public class RydePgpCompressionOutputStream extends ImprovedOutputStream {
public RydePgpCompressionOutputStream(
@Provided @Config("rdeRydeBufferSize") Integer bufferSize,
@WillNotClose OutputStream os) {
super(createDelegate(bufferSize, os));
super("RydePgpCompressionOutputStream", createDelegate(bufferSize, os));
}
private static OutputStream createDelegate(int bufferSize, OutputStream os) {

View file

@ -95,7 +95,7 @@ public class RydePgpEncryptionOutputStream extends ImprovedOutputStream {
@Provided @Config("rdeRydeBufferSize") Integer bufferSize,
@WillNotClose OutputStream os,
PGPPublicKey receiverKey) {
super(createDelegate(bufferSize, os, receiverKey));
super("RydePgpEncryptionOutputStream", createDelegate(bufferSize, os, receiverKey));
}
private static

View file

@ -51,7 +51,7 @@ public class RydePgpFileOutputStream extends ImprovedOutputStream {
@WillNotClose OutputStream os,
DateTime modified,
String filename) {
super(createDelegate(bufferSize, os, modified, filename));
super("RydePgpFileOutputStream", createDelegate(bufferSize, os, modified, filename));
}
private static OutputStream

View file

@ -56,7 +56,7 @@ public class RydePgpSigningOutputStream extends ImprovedOutputStream {
public RydePgpSigningOutputStream(
@WillNotClose OutputStream os,
PGPKeyPair signingKey) {
super(os, false, -1);
super("RydePgpSigningOutputStream", os, false);
try {
signer = new PGPSignatureGenerator(
new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));

View file

@ -30,6 +30,8 @@ import org.joda.time.DateTime;
@AutoFactory(allowSubclasses = true)
public class RydeTarOutputStream extends ImprovedOutputStream {
private final long expectedSize;
/**
* Creates a new instance that outputs a tar archive.
*
@ -42,8 +44,9 @@ public class RydeTarOutputStream extends ImprovedOutputStream {
*/
public RydeTarOutputStream(
@WillNotClose OutputStream os, long size, DateTime modified, String filename) {
super(os, false, size);
super("RydeTarOutputStream", os, false);
checkArgument(size >= 0);
this.expectedSize = size;
checkArgument(filename.endsWith(".xml"),
"Ryde expects tar archive to contain a filename with an '.xml' extension.");
try {
@ -61,7 +64,13 @@ public class RydeTarOutputStream extends ImprovedOutputStream {
/** Writes the end of archive marker. */
@Override
public void onClose() throws IOException {
if (getBytesWritten() != expectedSize) {
throw new IOException(
String.format(
"RydeTarOutputStream expected %,d bytes, but got %,d bytes",
expectedSize, getBytesWritten()));
}
// Round up to a 512-byte boundary and another 1024-bytes to indicate end of archive.
write(new byte[1024 + 512 - (int) (getBytesWritten() % 512L)]);
out.write(new byte[1024 + 512 - (int) (getBytesWritten() % 512L)]);
}
}