Faster and cleaner way to obtain UTF-8 encoded form.

Instead of specifying character encoding by name, the faster, cleaner,
and safer way is to use StandardCharsets.UTF_8.

Bug: 27461702
Change-Id: I897284d3ceeb44a21cc74de09a9b25f6aec8c205
This commit is contained in:
Alex Klyubin
2016-06-14 14:18:21 -07:00
parent cf89865b95
commit d4761a19b8
5 changed files with 10 additions and 15 deletions

View File

@@ -16,7 +16,7 @@
package com.android.apksigner.core.internal.jar;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -220,11 +220,7 @@ public class ManifestParser {
if (lineLengthBytes == 0) {
return "";
}
try {
return new String(mManifest, startOffset, lineLengthBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 character encoding not supported", e);
}
return new String(mManifest, startOffset, lineLengthBytes, StandardCharsets.UTF_8);
}

View File

@@ -18,6 +18,7 @@ package com.android.apksigner.core.internal.jar;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
@@ -81,7 +82,7 @@ public abstract class ManifestWriter {
}
private static void writeLine(OutputStream out, String line) throws IOException {
byte[] lineBytes = line.getBytes("UTF-8");
byte[] lineBytes = line.getBytes(StandardCharsets.UTF_8);
int offset = 0;
int remaining = lineBytes.length;
boolean firstLine = true;

View File

@@ -18,9 +18,9 @@ package com.android.apksigner.core.internal.zip;
import com.android.apksigner.core.zip.ZipFormatException;
import java.io.UnsupportedEncodingException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
/**
@@ -168,11 +168,7 @@ public class CentralDirectoryRecord {
record.position(originalPosition);
}
}
try {
return new String(nameBytes, nameBytesOffset, nameLengthBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 character encoding not supported", e);
}
return new String(nameBytes, nameBytesOffset, nameLengthBytes, StandardCharsets.UTF_8);
}
private static class ByLocalFileHeaderOffsetComparator

View File

@@ -20,6 +20,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
@@ -93,7 +94,7 @@ public class LocalFileHeader {
// exhibited when reading an APK for the purposes of verifying its signatures.
String entryName = cdRecord.getName();
byte[] cdNameBytes = entryName.getBytes("UTF-8");
byte[] cdNameBytes = entryName.getBytes(StandardCharsets.UTF_8);
int headerSizeWithName = HEADER_SIZE_BYTES + cdNameBytes.length;
long localFileHeaderOffsetInArchive = cdRecord.getLocalFileHeaderOffset();
long headerEndInArchive = localFileHeaderOffsetInArchive + headerSizeWithName;

View File

@@ -57,6 +57,7 @@ import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyFactory;
@@ -717,7 +718,7 @@ class SignApk {
// archive comment, so that tools that display the comment
// (hopefully) show something sensible.
// TODO: anything more useful we can put in this message?
byte[] message = "signed by SignApk".getBytes("UTF-8");
byte[] message = "signed by SignApk".getBytes(StandardCharsets.UTF_8);
temp.write(message);
temp.write(0);