google-nomulus/java/google/registry/rde/RydeCompression.java
guyben 260a6fb23b Move the RDE compression to a dedicated file
Merges the compressor creation between RyDE and Ghostryde. Note that GhostRyde
will now compress with ZIP rather than the previous ZLIB. This is backwards
compatible because the decompression algorithm works with either, so files
created by the old version (with ZLIB) can still be opened by the new version,
and vice-versa.

The new file - RydeCompression.java - is a merge of the removed functions in Ghostryde.java and the RydePgpCompressionOutputStream.java.

This is one of a series of CLs - each merging a single "part" of the encoding.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205102150
2018-08-10 13:44:25 -04:00

80 lines
2.9 KiB
Java

// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.rde;
import static org.bouncycastle.bcpg.CompressionAlgorithmTags.ZIP;
import google.registry.util.ImprovedInputStream;
import google.registry.util.ImprovedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.CheckReturnValue;
import javax.annotation.WillNotClose;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPException;
/**
* OpenPGP compression service that wraps an {@link OutputStream}.
*
* <p>This uses the ZIP compression algorithm per the ICANN escrow specification.
*/
final class RydeCompression {
private static final int BUFFER_SIZE = 64 * 1024;
/**
* Compression algorithm to use when creating RyDE files.
*
* <p>We're going to use ZIP because that's what the spec mandates.
*
* @see org.bouncycastle.bcpg.CompressionAlgorithmTags
*/
private static final int COMPRESSION_ALGORITHM = ZIP;
/**
* Creates an OutputStream that compresses the data.
*
* <p>TODO(b/110465964): document where the input comes from / output goes to. Something like
* documenting that os is the result of openEncryptor and the result goes into openFileEncoder.
*/
@CheckReturnValue
static ImprovedOutputStream openCompressor(@WillNotClose OutputStream os) {
try {
return new ImprovedOutputStream(
"RydeCompressor",
new PGPCompressedDataGenerator(COMPRESSION_ALGORITHM).open(os, new byte[BUFFER_SIZE]));
} catch (IOException | PGPException e) {
throw new RuntimeException(e);
}
}
/**
* Creates an InputStream that decompresses the data.
*
* <p>TODO(b/110465964): document where the input comes from / output goes to. Something like
* documenting that input is the result of openDecryptor and the result goes into openFileDecoder.
*/
@CheckReturnValue
static ImprovedInputStream openDecompressor(@WillNotClose InputStream input) {
try {
PGPCompressedData compressed = PgpUtils.readSinglePgpObject(input, PGPCompressedData.class);
return new ImprovedInputStream("RydeDecompressor", compressed.getDataStream());
} catch (PGPException e) {
throw new RuntimeException(e);
}
}
}