diff --git a/java/google/registry/rde/Ghostryde.java b/java/google/registry/rde/Ghostryde.java index 3ae13fad1..4ff5cbf46 100644 --- a/java/google/registry/rde/Ghostryde.java +++ b/java/google/registry/rde/Ghostryde.java @@ -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); } } diff --git a/java/google/registry/rde/RydePgpCompressionOutputStream.java b/java/google/registry/rde/RydePgpCompressionOutputStream.java index 1784744ef..9876bfa99 100644 --- a/java/google/registry/rde/RydePgpCompressionOutputStream.java +++ b/java/google/registry/rde/RydePgpCompressionOutputStream.java @@ -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) { diff --git a/java/google/registry/rde/RydePgpEncryptionOutputStream.java b/java/google/registry/rde/RydePgpEncryptionOutputStream.java index d1eb8a69a..85b1d3078 100644 --- a/java/google/registry/rde/RydePgpEncryptionOutputStream.java +++ b/java/google/registry/rde/RydePgpEncryptionOutputStream.java @@ -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 diff --git a/java/google/registry/rde/RydePgpFileOutputStream.java b/java/google/registry/rde/RydePgpFileOutputStream.java index f6c165975..6f1887ef4 100644 --- a/java/google/registry/rde/RydePgpFileOutputStream.java +++ b/java/google/registry/rde/RydePgpFileOutputStream.java @@ -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 diff --git a/java/google/registry/rde/RydePgpSigningOutputStream.java b/java/google/registry/rde/RydePgpSigningOutputStream.java index 1c82fd595..0e8b1aaf4 100644 --- a/java/google/registry/rde/RydePgpSigningOutputStream.java +++ b/java/google/registry/rde/RydePgpSigningOutputStream.java @@ -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)); diff --git a/java/google/registry/rde/RydeTarOutputStream.java b/java/google/registry/rde/RydeTarOutputStream.java index 8951a2df2..eb2671946 100644 --- a/java/google/registry/rde/RydeTarOutputStream.java +++ b/java/google/registry/rde/RydeTarOutputStream.java @@ -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)]); } } diff --git a/java/google/registry/util/ImprovedInputStream.java b/java/google/registry/util/ImprovedInputStream.java index e9540ba8d..f5a35d777 100644 --- a/java/google/registry/util/ImprovedInputStream.java +++ b/java/google/registry/util/ImprovedInputStream.java @@ -14,7 +14,6 @@ package google.registry.util; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.flogger.FluentLogger; @@ -47,18 +46,17 @@ public class ImprovedInputStream extends FilterInputStream { private long count; private long mark = -1; - private final long expected; private final boolean shouldClose; + private final String name; - public ImprovedInputStream(@WillCloseWhenClosed InputStream out) { - this(out, true, -1); + public ImprovedInputStream(String name, @WillCloseWhenClosed InputStream out) { + this(name, out, true); } - public ImprovedInputStream(InputStream in, boolean shouldClose, long expected) { + public ImprovedInputStream(String name, InputStream in, boolean shouldClose) { super(checkNotNull(in, "in")); - checkArgument(expected >= -1, "expected >= 0 or -1"); this.shouldClose = shouldClose; - this.expected = expected; + this.name = name; } @Override @@ -126,16 +124,12 @@ public class ImprovedInputStream extends FilterInputStream { if (in == null) { return; } - logger.atInfo().log("%s closed with %,d bytes read", getClass().getSimpleName(), count); - if (expected != -1 && count != expected) { - throw new IOException(String.format("%s expected %,d bytes but read %,d bytes", - getClass().getCanonicalName(), expected, count)); - } onClose(); if (shouldClose) { in.close(); } in = null; + logger.atInfo().log("%s closed with %,d bytes read", name, count); } /** diff --git a/java/google/registry/util/ImprovedOutputStream.java b/java/google/registry/util/ImprovedOutputStream.java index b75e6eef1..09060b060 100644 --- a/java/google/registry/util/ImprovedOutputStream.java +++ b/java/google/registry/util/ImprovedOutputStream.java @@ -14,7 +14,6 @@ package google.registry.util; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.flogger.FluentLogger; @@ -46,18 +45,17 @@ public class ImprovedOutputStream extends FilterOutputStream { private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private long count; - private final long expected; private final boolean shouldClose; + private final String name; - public ImprovedOutputStream(@WillCloseWhenClosed OutputStream out) { - this(out, true, -1); + public ImprovedOutputStream(String name, @WillCloseWhenClosed OutputStream out) { + this(name, out, true); } - public ImprovedOutputStream(OutputStream out, boolean shouldClose, long expected) { + public ImprovedOutputStream(String name, OutputStream out, boolean shouldClose) { super(checkNotNull(out, "out")); - checkArgument(expected >= -1, "expected >= 0 or -1"); this.shouldClose = shouldClose; - this.expected = expected; + this.name = name; } /** Returns the number of bytes that have been written to this stream thus far. */ @@ -103,18 +101,14 @@ public class ImprovedOutputStream extends FilterOutputStream { try { flush(); } catch (IOException e) { - logger.atWarning().withCause(e).log("%s.flush() failed", getClass().getSimpleName()); - } - logger.atInfo().log("%s closed with %,d bytes written", getClass().getSimpleName(), count); - if (expected != -1 && count != expected) { - throw new IOException(String.format( - "%s expected %,d bytes but got %,d bytes", getClass().getSimpleName(), expected, count)); + logger.atWarning().withCause(e).log("flush() failed for %s", name); } onClose(); if (shouldClose) { out.close(); } out = null; + logger.atInfo().log("%s closed with %,d bytes written", name, count); } /**