Skip to content

Commit

Permalink
Initialize our Charsets constants using the fields in `StandardChar…
Browse files Browse the repository at this point in the history
…sets`.

(followup to cl/655152755, in which I explain why this is safe)

(split off from kak's cl/655162746)

On the JVM, `StandardCharsets` appears to [bypass](https://github.com/openjdk/jdk/blob/5a8861a3a1b436ce7414176c095c58c9a1e038d6/src/java.base/share/classes/java/nio/charset/StandardCharsets.java#L50) the usual [lookup logic](https://github.com/openjdk/jdk/blob/5a8861a3a1b436ce7414176c095c58c9a1e038d6/src/java.base/share/classes/java/nio/charset/Charset.java#L483), so that save a tiny bit of time (and avoid trashing the one-element cache).

On Android, `StandardCharsets` [does the same thing as `Charsets` did up until now](https://cs.android.com/android/platform/superproject/main/+/main:libcore/ojluni/src/main/java/java/nio/charset/StandardCharsets.java;l=52;drc=1fe41f6f569bb17811d7b7ca13a0769ddead2046), but at least we'll now have only one class that does that work instead of doing it twice.

RELNOTES=n/a
PiperOrigin-RevId: 656144236
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 25, 2024
1 parent 5827422 commit e2b7a33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
25 changes: 13 additions & 12 deletions android/guava/src/com/google/common/base/Charsets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Contains constant definitions for the six standard {@link Charset} instances, which are
Expand All @@ -40,64 +41,64 @@ private Charsets() {}
* US-ASCII: seven-bit ASCII, the Basic Latin block of the Unicode character set (ISO646-US).
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#US_ASCII} instead.
* {@link StandardCharsets#US_ASCII} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset US_ASCII = Charset.forName("US-ASCII");
public static final Charset US_ASCII = StandardCharsets.US_ASCII;

/**
* ISO-8859-1: ISO Latin Alphabet Number 1 (ISO-LATIN-1).
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
* {@link StandardCharsets#ISO_8859_1} instead.
*
*/
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;

/**
* UTF-8: eight-bit UCS Transformation Format.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_8} instead.
* {@link StandardCharsets#UTF_8} instead.
*
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset UTF_8 = StandardCharsets.UTF_8;

/**
* UTF-16BE: sixteen-bit UCS Transformation Format, big-endian byte order.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16BE} instead.
* {@link StandardCharsets#UTF_16BE} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;

/**
* UTF-16LE: sixteen-bit UCS Transformation Format, little-endian byte order.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16LE} instead.
* {@link StandardCharsets#UTF_16LE} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;

/**
* UTF-16: sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order
* mark.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16} instead.
* {@link StandardCharsets#UTF_16} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16 = Charset.forName("UTF-16");
public static final Charset UTF_16 = StandardCharsets.UTF_16;

/*
* Please do not add new Charset references to this class, unless those character encodings are
Expand Down
25 changes: 13 additions & 12 deletions guava/src/com/google/common/base/Charsets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Contains constant definitions for the six standard {@link Charset} instances, which are
Expand All @@ -40,64 +41,64 @@ private Charsets() {}
* US-ASCII: seven-bit ASCII, the Basic Latin block of the Unicode character set (ISO646-US).
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#US_ASCII} instead.
* {@link StandardCharsets#US_ASCII} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset US_ASCII = Charset.forName("US-ASCII");
public static final Charset US_ASCII = StandardCharsets.US_ASCII;

/**
* ISO-8859-1: ISO Latin Alphabet Number 1 (ISO-LATIN-1).
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
* {@link StandardCharsets#ISO_8859_1} instead.
*
*/
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;

/**
* UTF-8: eight-bit UCS Transformation Format.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_8} instead.
* {@link StandardCharsets#UTF_8} instead.
*
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset UTF_8 = StandardCharsets.UTF_8;

/**
* UTF-16BE: sixteen-bit UCS Transformation Format, big-endian byte order.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16BE} instead.
* {@link StandardCharsets#UTF_16BE} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;

/**
* UTF-16LE: sixteen-bit UCS Transformation Format, little-endian byte order.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16LE} instead.
* {@link StandardCharsets#UTF_16LE} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;

/**
* UTF-16: sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order
* mark.
*
* <p><b>Note:</b> this constant is now unnecessary and should be treated as deprecated; use
* {@link java.nio.charset.StandardCharsets#UTF_16} instead.
* {@link StandardCharsets#UTF_16} instead.
*
*/
@J2ktIncompatible
@GwtIncompatible // Charset not supported by GWT
public static final Charset UTF_16 = Charset.forName("UTF-16");
public static final Charset UTF_16 = StandardCharsets.UTF_16;

/*
* Please do not add new Charset references to this class, unless those character encodings are
Expand Down

0 comments on commit e2b7a33

Please sign in to comment.