Commit 2a6f23ff authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Add java.nio.charsets.StandardCharsets.

Bug: 3484927
Change-Id: I5820267491b850b8fcc696fa48962710de123009
parent 52ac4053
......@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.SocketException;
import java.nio.charset.Charsets;
/**
* Mechanism to let threads set restrictions on what code is allowed
......
......@@ -16,7 +16,7 @@
package java.net;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import static libcore.io.OsConstants.*;
......@@ -29,7 +29,7 @@ public final class InetUnixAddress extends InetAddress {
* Constructs an AF_UNIX InetAddress for the given path.
*/
public InetUnixAddress(String path) {
this(path.getBytes(Charsets.UTF_8));
this(path.getBytes(StandardCharsets.UTF_8));
}
/**
......@@ -43,6 +43,6 @@ public final class InetUnixAddress extends InetAddress {
* Returns a string form of this InetAddress.
*/
@Override public String toString() {
return "InetUnixAddress[" + new String(ipaddress, Charsets.UTF_8) + "]";
return "InetUnixAddress[" + new String(ipaddress, StandardCharsets.UTF_8) + "]";
}
}
......@@ -18,7 +18,7 @@
package java.net;
import java.nio.ByteOrder;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import libcore.io.Memory;
class Socks4Message {
......@@ -188,7 +188,7 @@ class Socks4Message {
while (index < lastIndex && (buffer[index] != 0)) {
index++;
}
return new String(buffer, offset, index - offset, Charsets.ISO_8859_1);
return new String(buffer, offset, index - offset, StandardCharsets.ISO_8859_1);
}
/**
......@@ -202,7 +202,7 @@ class Socks4Message {
* Put a string into the buffer at the offset given.
*/
private void setString(int offset, int maxLength, String theString) {
byte[] stringBytes = theString.getBytes(Charsets.ISO_8859_1);
byte[] stringBytes = theString.getBytes(StandardCharsets.ISO_8859_1);
int length = Math.min(stringBytes.length, maxLength);
System.arraycopy(stringBytes, 0, buffer, offset, length);
buffer[offset + length] = 0;
......
......@@ -26,7 +26,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.SecureClassLoader;
......@@ -76,7 +76,7 @@ public class URLClassLoader extends SecureClassLoader {
String parentURLString = getParentURL(url).toExternalForm();
String prefix = "jar:" + parentURLString + "/";
is = jf.getInputStream(indexEntry);
in = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));
in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
HashMap<String, ArrayList<URL>> pre_map = new HashMap<String, ArrayList<URL>>();
// Ignore the 2 first lines (index version)
if (in.readLine() == null) return null;
......
......@@ -19,7 +19,7 @@ package java.net;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import libcore.net.UriCodec;
/**
......@@ -47,7 +47,7 @@ public class URLEncoder {
*/
@Deprecated
public static String encode(String s) {
return ENCODER.encode(s, Charsets.UTF_8);
return ENCODER.encode(s, StandardCharsets.UTF_8);
}
/**
......
/*
* Copyright (C) 2010 The Android Open Source Project
*
* 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 java.nio.charset;
/**
* Convenient access to the most important built-in charsets.
* @since 1.7
*/
public final class StandardCharsets {
private StandardCharsets() {
}
/**
* The ISO-8859-1 charset.
*/
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
/**
* The US-ASCII charset.
*/
public static final Charset US_ASCII = Charset.forName("US-ASCII");
/**
* The UTF-8 charset.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
/**
* The UTF-16 charset.
*/
public static final Charset UTF_16 = Charset.forName("UTF-16");
/**
* The UTF-16BE (big-endian) charset.
*/
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
/**
* The UTF-16LE (little-endian) charset.
*/
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
}
......@@ -24,8 +24,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charsets;
import static java.nio.charset.Charsets.UTF_8;
import java.nio.charset.StandardCharsets;
import libcore.io.IoUtils;
/**
......@@ -518,7 +517,7 @@ public abstract class ResourceBundle {
: ClassLoader.getSystemResourceAsStream(fileName);
if (stream != null) {
try {
bundle = new PropertyResourceBundle(new InputStreamReader(stream, UTF_8));
bundle = new PropertyResourceBundle(new InputStreamReader(stream, StandardCharsets.UTF_8));
bundle.setLocale(locale);
} catch (IOException ignored) {
} finally {
......
......@@ -18,7 +18,7 @@
package java.util.jar;
import java.io.IOException;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
......@@ -110,7 +110,7 @@ class InitManifest {
continue;
}
String name = new String(buf, mark, pos - mark - 1, Charsets.US_ASCII);
String name = new String(buf, mark, pos - mark - 1, StandardCharsets.US_ASCII);
if (buf[pos++] != ' ') {
throw new IOException(String.format("Invalid value for attribute '%s'", name));
......@@ -164,6 +164,6 @@ class InitManifest {
}
valueBuffer.write(buf, mark, last - mark);
value = valueBuffer.toString(Charsets.UTF_8);
value = valueBuffer.toString(StandardCharsets.UTF_8);
}
}
......@@ -20,7 +20,7 @@ package java.util.jar;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
......@@ -206,7 +206,7 @@ class JarVerifier {
if (hash == null) {
continue;
}
byte[] hashBytes = hash.getBytes(Charsets.ISO_8859_1);
byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1);
try {
return new VerifierEntry(name, MessageDigest.getInstance(algorithm), hashBytes,
......@@ -408,7 +408,7 @@ class JarVerifier {
md.update(data, start, end - start);
}
byte[] b = md.digest();
byte[] hashBytes = hash.getBytes(Charsets.ISO_8859_1);
byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1);
return MessageDigest.isEqual(b, Base64.decode(hashBytes));
}
return ignorable;
......
......@@ -25,8 +25,8 @@ import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.Charsets;
import java.nio.charset.CoderResult;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
......@@ -302,7 +302,7 @@ public class Manifest implements Cloneable {
* If an error occurs writing the {@code Manifest}.
*/
static void write(Manifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = Charsets.UTF_8.newEncoder();
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
......@@ -339,7 +339,7 @@ public class Manifest implements Cloneable {
private static void writeEntry(OutputStream os, Attributes.Name name,
String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
String nameString = name.getName();
os.write(nameString.getBytes(Charsets.US_ASCII));
os.write(nameString.getBytes(StandardCharsets.US_ASCII));
os.write(VALUE_SEPARATOR);
encoder.reset();
......
......@@ -19,7 +19,7 @@ package java.util.prefs;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.EventListener;
import java.util.EventObject;
......@@ -441,7 +441,7 @@ public abstract class AbstractPreferences extends Preferences {
return EmptyArray.BYTE;
}
try {
byte[] bavalue = svalue.getBytes(Charsets.US_ASCII);
byte[] bavalue = svalue.getBytes(StandardCharsets.US_ASCII);
if (bavalue.length % 4 != 0) {
return deflt;
}
......
......@@ -20,7 +20,7 @@ package java.util.zip;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
......@@ -184,7 +184,7 @@ public class ZipEntry implements ZipConstants, Cloneable {
return;
}
byte[] commentBytes = comment.getBytes(Charsets.UTF_8);
byte[] commentBytes = comment.getBytes(StandardCharsets.UTF_8);
if (commentBytes.length > 0xffff) {
throw new IllegalArgumentException("Comment too long: " + commentBytes.length);
}
......@@ -383,14 +383,14 @@ public class ZipEntry implements ZipConstants, Cloneable {
byte[] nameBytes = new byte[nameLength];
Streams.readFully(in, nameBytes, 0, nameBytes.length);
name = new String(nameBytes, 0, nameBytes.length, Charsets.UTF_8);
name = new String(nameBytes, 0, nameBytes.length, StandardCharsets.UTF_8);
// The RI has always assumed UTF-8. (If GPBF_UTF8_FLAG isn't set, the encoding is
// actually IBM-437.)
if (commentByteCount > 0) {
byte[] commentBytes = new byte[commentByteCount];
Streams.readFully(in, commentBytes, 0, commentByteCount);
comment = new String(commentBytes, 0, commentBytes.length, Charsets.UTF_8);
comment = new String(commentBytes, 0, commentBytes.length, StandardCharsets.UTF_8);
}
if (extraLength > 0) {
......
......@@ -20,7 +20,7 @@ package java.util.zip;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import libcore.util.EmptyArray;
......@@ -182,7 +182,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
String comment = currentEntry.getComment();
byte[] commentBytes = EmptyArray.BYTE;
if (comment != null) {
commentBytes = comment.getBytes(Charsets.UTF_8);
commentBytes = comment.getBytes(StandardCharsets.UTF_8);
}
writeShort(cDir, commentBytes.length); // Comment length.
writeShort(cDir, 0); // Disk Start
......@@ -294,7 +294,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
// TODO: support Zip64.
throw new ZipException("Too many entries for the zip file format's 16-bit entry count");
}
nameBytes = ze.name.getBytes(Charsets.UTF_8);
nameBytes = ze.name.getBytes(StandardCharsets.UTF_8);
nameLength = nameBytes.length;
if (nameLength > 0xffff) {
throw new IllegalArgumentException("Name too long: " + nameLength + " UTF-8 bytes");
......@@ -353,7 +353,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
return;
}
byte[] newCommentBytes = comment.getBytes(Charsets.UTF_8);
byte[] newCommentBytes = comment.getBytes(StandardCharsets.UTF_8);
if (newCommentBytes.length > 0xffff) {
throw new IllegalArgumentException("Comment too long: " + newCommentBytes.length + " bytes");
}
......
......@@ -21,7 +21,7 @@
package libcore.io;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import libcore.util.EmptyArray;
/**
......@@ -156,6 +156,6 @@ public final class Base64 {
out[index++] = '=';
break;
}
return new String(out, 0, index, Charsets.US_ASCII);
return new String(out, 0, index, StandardCharsets.US_ASCII);
}
}
......@@ -22,7 +22,7 @@ import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import static libcore.io.OsConstants.*;
......@@ -110,7 +110,7 @@ public final class IoUtils {
* Returns the contents of 'path' as a string. The contents are assumed to be UTF-8.
*/
public static String readFileAsString(String path) throws IOException {
return readFileAsBytes(path).toString(Charsets.UTF_8);
return readFileAsBytes(path).toString(StandardCharsets.UTF_8);
}
private static UnsafeByteSequence readFileAsBytes(String path) throws IOException {
......
......@@ -22,7 +22,7 @@ import java.io.EOFException;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
/**
* Buffers input from an {@link InputStream} for reading lines.
......@@ -78,7 +78,7 @@ public class StrictLineReader implements Closeable {
* @throws IllegalArgumentException for negative or zero {@code capacity}.
*/
public StrictLineReader(InputStream in, int capacity) {
this(in, capacity, Charsets.US_ASCII);
this(in, capacity, StandardCharsets.US_ASCII);
}
/**
......@@ -114,8 +114,8 @@ public class StrictLineReader implements Closeable {
if (capacity < 0) {
throw new IllegalArgumentException("capacity <= 0");
}
if (!(charset.equals(Charsets.US_ASCII) || charset.equals(Charsets.UTF_8) ||
charset.equals(Charsets.ISO_8859_1))) {
if (!(charset.equals(StandardCharsets.US_ASCII) || charset.equals(StandardCharsets.UTF_8) ||
charset.equals(StandardCharsets.ISO_8859_1))) {
throw new IllegalArgumentException("Unsupported encoding");
}
......@@ -240,4 +240,3 @@ public class StrictLineReader implements Closeable {
end = result;
}
}
......@@ -20,7 +20,7 @@ package libcore.net;
import java.io.ByteArrayOutputStream;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
/**
* Encodes and decodes {@code application/x-www-form-urlencoded} content.
......@@ -135,11 +135,11 @@ public abstract class UriCodec {
}
public final void appendEncoded(StringBuilder builder, String s) {
appendEncoded(builder, s, Charsets.UTF_8, false);
appendEncoded(builder, s, StandardCharsets.UTF_8, false);
}
public final void appendPartiallyEncoded(StringBuilder builder, String s) {
appendEncoded(builder, s, Charsets.UTF_8, true);
appendEncoded(builder, s, StandardCharsets.UTF_8, true);
}
/**
......@@ -203,7 +203,7 @@ public abstract class UriCodec {
}
public static String decode(String s) {
return decode(s, false, Charsets.UTF_8, true);
return decode(s, false, StandardCharsets.UTF_8, true);
}
private static void appendHex(StringBuilder builder, String s, Charset charset) {
......
......@@ -35,7 +35,7 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Iterator;
......@@ -350,7 +350,7 @@ public class FtpURLConnection extends URLConnection {
}
code[i] = (byte) tmp;
}
replyCode = new String(code, 0, code.length, Charsets.ISO_8859_1);
replyCode = new String(code, 0, code.length, StandardCharsets.ISO_8859_1);
boolean multiline = false;
if (ctrlInput.read() == '-') {
......@@ -497,6 +497,6 @@ public class FtpURLConnection extends URLConnection {
}
private void write(String command) throws IOException {
ctrlOutput.write(command.getBytes(Charsets.ISO_8859_1));
ctrlOutput.write(command.getBytes(StandardCharsets.ISO_8859_1));
}
}
......@@ -21,7 +21,7 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -111,11 +111,11 @@ public final class ZoneInfoDB {
byte[] tzdata_version = new byte[12];
it.readByteArray(tzdata_version, 0, tzdata_version.length);
String magic = new String(tzdata_version, 0, 6, Charsets.US_ASCII);
String magic = new String(tzdata_version, 0, 6, StandardCharsets.US_ASCII);
if (!magic.equals("tzdata") || tzdata_version[11] != 0) {
throw new RuntimeException("bad tzdata magic: " + Arrays.toString(tzdata_version));
}
version = new String(tzdata_version, 6, 5, Charsets.US_ASCII);
version = new String(tzdata_version, 6, 5, StandardCharsets.US_ASCII);
int index_offset = it.readInt();
int data_offset = it.readInt();
......@@ -129,7 +129,7 @@ public final class ZoneInfoDB {
byte[] bytes = new byte[zoneTabSize];
it.seek(zoneTabOffset);
it.readByteArray(bytes, 0, bytes.length);
zoneTab = new String(bytes, 0, bytes.length, Charsets.US_ASCII);
zoneTab = new String(bytes, 0, bytes.length, StandardCharsets.US_ASCII);
}
private void readIndex(BufferIterator it, int indexOffset, int dataOffset) {
......
......@@ -23,7 +23,7 @@
package org.apache.harmony.security.asn1;
import java.io.IOException;
import java.nio.charset.Charsets;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
......@@ -99,7 +99,7 @@ public final class ASN1GeneralizedTime extends ASN1Time {
temp = temp.substring(0, currLength);
}
out.content = (temp + "Z").getBytes(Charsets.UTF_8);
out.content = (temp + "Z").getBytes(StandardCharsets.UTF_8);
out.length = ((byte[]) out.content).length;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment