1 // Copyright 2016 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.archivepatcher.shared; 16 17 import java.io.ByteArrayInputStream; 18 import java.io.ByteArrayOutputStream; 19 import java.io.IOException; 20 import java.io.UnsupportedEncodingException; 21 22 /** 23 * Data for one entry in the zip returned by {@link UnitTestZipArchive#makeTestZip()}. 24 */ 25 public class UnitTestZipEntry { 26 /** 27 * The path under which the data is located in the archive. 28 */ 29 public final String path; 30 31 /** 32 * The compression level of the entry. 33 */ 34 public final int level; 35 36 /** 37 * The binary content of the entry. 38 */ 39 public final String content; 40 41 /** 42 * Optional comment, as an ASCII string. 43 */ 44 public final String comment; 45 46 /** 47 * Whether or not to use nowrap. 48 */ 49 public final boolean nowrap; 50 51 /** 52 * Creates a new entry with nowrap=true. 53 * @param path the path under which the data is located in the archive 54 * @param level the compression level of the entry 55 * @param content the binary content of the entry, as an ASCII string 56 * @param comment optional comment, as an ASCII string 57 */ UnitTestZipEntry(String path, int level, String content, String comment)58 public UnitTestZipEntry(String path, int level, String content, String comment) { 59 this(path, level, true, content, comment); 60 } 61 62 /** 63 * Creates a new entry. 64 * 65 * @param path the path under which the data is located in the archive 66 * @param level the compression level of the entry 67 * @param nowrap the wrapping mode (false to wrap the entry like gzip, true otherwise) 68 * @param content the binary content of the entry, as an ASCII string 69 * @param comment optional comment, as an ASCII string 70 */ UnitTestZipEntry(String path, int level, boolean nowrap, String content, String comment)71 public UnitTestZipEntry(String path, int level, boolean nowrap, String content, String comment) { 72 this.path = path; 73 this.level = level; 74 this.nowrap = nowrap; 75 this.content = content; 76 this.comment = comment; 77 } 78 79 /** 80 * Returns the uncompressed content of the entry as a byte array for unit test simplicity. If the 81 * level is 0, this is the same as the actual array of bytes that will be present in the zip 82 * archive. If the level is not 0, this is the result of uncompressed the bytes that are present 83 * in the zip archive for this entry. 84 * @return as described 85 */ getUncompressedBinaryContent()86 public byte[] getUncompressedBinaryContent() { 87 try { 88 return content.getBytes("US-ASCII"); 89 } catch (UnsupportedEncodingException e) { 90 throw new RuntimeException("System doesn't support US-ASCII"); // Not likely 91 } 92 } 93 94 /** 95 * Returns the compressed form of the content, according to the level, that should be found in the 96 * zip archive. If the level is 0 (store, i.e. not compressed) this is the same as calling 97 * {@link #getUncompressedBinaryContent()}. 98 * @return the content, as a byte array 99 */ getCompressedBinaryContent()100 public byte[] getCompressedBinaryContent() { 101 if (level == 0) { 102 return getUncompressedBinaryContent(); 103 } 104 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 105 DeflateCompressor compressor = new DeflateCompressor(); 106 compressor.setCompressionLevel(level); 107 compressor.setNowrap(nowrap); 108 try { 109 compressor.compress(new ByteArrayInputStream(getUncompressedBinaryContent()), buffer); 110 } catch (IOException e) { 111 throw new RuntimeException(e); // Shouldn't happen as this is all in-memory 112 } 113 return buffer.toByteArray(); 114 } 115 } 116