1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 android.util.jar;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import libcore.io.Streams;
/**
* The {@code StrictJarManifest} class is used to obtain attribute information for a
* {@code StrictJarFile} and its entries.
*
* @hide
*/
public class StrictJarManifest implements Cloneable {
static final int LINE_LENGTH_LIMIT = 72;
private static final byte[] LINE_SEPARATOR = new byte[] { '\r', '\n' };
private static final byte[] VALUE_SEPARATOR = new byte[] { ':', ' ' };
/** The attribute name "Name". */
static final Attributes.Name ATTRIBUTE_NAME_NAME = new Attributes.Name("Name");
private final Attributes mainAttributes;
private final HashMap<String, Attributes> entries;
static final class Chunk {
final int start;
final int end;
Chunk(int start, int end) {
this.start = start;
this.end = end;
}
}
private HashMap<String, Chunk> chunks;
/**
* The end of the main attributes section in the manifest is needed in
* verification.
*/
private int mainEnd;
/**
* Creates a new {@code StrictJarManifest} instance.
*/
public StrictJarManifest() {
entries = new HashMap<String, Attributes>();
mainAttributes = new Attributes();
}
/**
* Creates a new {@code StrictJarManifest} instance using the attributes obtained
* from the input stream.
*
* @param is
* {@code InputStream} to parse for attributes.
* @throws IOException
* if an IO error occurs while creating this {@code StrictJarManifest}
*/
public StrictJarManifest(InputStream is) throws IOException {
this();
read(Streams.readFully(is));
}
/**
* Creates a new {@code StrictJarManifest} instance. The new instance will have the
* same attributes as those found in the parameter {@code StrictJarManifest}.
*
* @param man
* {@code StrictJarManifest} instance to obtain attributes from.
*/
@SuppressWarnings("unchecked")
public StrictJarManifest(StrictJarManifest man) {
mainAttributes = (Attributes) man.mainAttributes.clone();
entries = (HashMap<String, Attributes>) ((HashMap<String, Attributes>) man
.getEntries()).clone();
}
StrictJarManifest(byte[] manifestBytes, boolean readChunks) throws IOException {
this();
if (readChunks) {
chunks = new HashMap<String, Chunk>();
}
read(manifestBytes);
}
/**
* Resets the both the main attributes as well as the entry attributes
* associated with this {@code StrictJarManifest}.
*/
public void clear() {
entries.clear();
mainAttributes.clear();
}
/**
* Returns the {@code Attributes} associated with the parameter entry
* {@code name}.
*
* @param name
* the name of the entry to obtain {@code Attributes} from.
* @return the Attributes for the entry or {@code null} if the entry does
* not exist.
*/
public Attributes getAttributes(String name) {
return getEntries().get(name);
}
/**
* Returns a map containing the {@code Attributes} for each entry in the
* {@code StrictJarManifest}.
*
* @return the map of entry attributes.
*/
public Map<String, Attributes> getEntries() {
return entries;
}
/**
* Returns the main {@code Attributes} of the {@code JarFile}.
*
* @return main {@code Attributes} associated with the source {@code
* JarFile}.
*/
public Attributes getMainAttributes() {
return mainAttributes;
}
/**
* Creates a copy of this {@code StrictJarManifest}. The returned {@code StrictJarManifest}
* will equal the {@code StrictJarManifest} from which it was cloned.
*
* @return a copy of this instance.
*/
@Override
public Object clone() {
return new StrictJarManifest(this);
}
/**
* Writes this {@code StrictJarManifest}'s name/attributes pairs to the given {@code OutputStream}.
* The {@code MANIFEST_VERSION} or {@code SIGNATURE_VERSION} attribute must be set before
* calling this method, or no attributes will be written.
*
* @throws IOException
* If an error occurs writing the {@code StrictJarManifest}.
*/
public void write(OutputStream os) throws IOException {
write(this, os);
}
/**
* Merges name/attribute pairs read from the input stream {@code is} into this manifest.
*
* @param is
* The {@code InputStream} to read from.
* @throws IOException
* If an error occurs reading the manifest.
*/
public void read(InputStream is) throws IOException {
read(Streams.readFullyNoClose(is));
}
private void read(byte[] buf) throws IOException {
if (buf.length == 0) {
return;
}
StrictJarManifestReader im = new StrictJarManifestReader(buf, mainAttributes);
mainEnd = im.getEndOfMainSection();
im.readEntries(entries, chunks);
}
/**
* Returns the hash code for this instance.
*
* @return this {@code StrictJarManifest}'s hashCode.
*/
@Override
public int hashCode() {
return mainAttributes.hashCode() ^ getEntries().hashCode();
}
/**
* Determines if the receiver is equal to the parameter object. Two {@code
* StrictJarManifest}s are equal if they have identical main attributes as well as
* identical entry attributes.
*
* @param o
* the object to compare against.
* @return {@code true} if the manifests are equal, {@code false} otherwise
*/
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o.getClass() != this.getClass()) {
return false;
}
if (!mainAttributes.equals(((StrictJarManifest) o).mainAttributes)) {
return false;
}
return getEntries().equals(((StrictJarManifest) o).getEntries());
}
Chunk getChunk(String name) {
return chunks.get(name);
}
void removeChunks() {
chunks = null;
}
int getMainAttributesEnd() {
return mainEnd;
}
/**
* Writes out the attribute information of the specified manifest to the
* specified {@code OutputStream}
*
* @param manifest
* the manifest to write out.
* @param out
* The {@code OutputStream} to write to.
* @throws IOException
* If an error occurs writing the {@code StrictJarManifest}.
*/
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
String version = manifest.mainAttributes.getValue(versionName);
if (version == null) {
versionName = Attributes.Name.SIGNATURE_VERSION;
version = manifest.mainAttributes.getValue(versionName);
}
if (version != null) {
writeEntry(out, versionName, version, encoder, buffer);
Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
if (!name.equals(versionName)) {
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
}
}
}
out.write(LINE_SEPARATOR);
Iterator<String> i = manifest.getEntries().keySet().iterator();
while (i.hasNext()) {
String key = i.next();
writeEntry(out, ATTRIBUTE_NAME_NAME, key, encoder, buffer);
Attributes attributes = manifest.entries.get(key);
Iterator<?> entries = attributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
writeEntry(out, name, attributes.getValue(name), encoder, buffer);
}
out.write(LINE_SEPARATOR);
}
}
private static void writeEntry(OutputStream os, Attributes.Name name,
String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
String nameString = name.toString();
os.write(nameString.getBytes(StandardCharsets.US_ASCII));
os.write(VALUE_SEPARATOR);
encoder.reset();
bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
CharBuffer cBuf = CharBuffer.wrap(value);
while (true) {
CoderResult r = encoder.encode(cBuf, bBuf, true);
if (CoderResult.UNDERFLOW == r) {
r = encoder.flush(bBuf);
}
os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
os.write(LINE_SEPARATOR);
if (CoderResult.UNDERFLOW == r) {
break;
}
os.write(' ');
bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
}
}
}
|