Replace usages of Charset.forName with Guava Charsets.

PiperOrigin-RevId: 317672619
This commit is contained in:
samrobinson 2020-06-22 17:44:44 +01:00 committed by Andrew Lewis
parent 1836f1df36
commit 836babd5d6
21 changed files with 73 additions and 75 deletions

View File

@ -85,23 +85,34 @@ public final class C {
public static final int BYTES_PER_FLOAT = 4;
/**
* The name of the ASCII charset.
* @deprecated Use {@link java.nio.charset.StandardCharsets} or {@link
* com.google.common.base.Charsets} instead.
*/
public static final String ASCII_NAME = "US-ASCII";
@Deprecated public static final String ASCII_NAME = "US-ASCII";
/**
* The name of the UTF-8 charset.
* @deprecated Use {@link java.nio.charset.StandardCharsets} or {@link
* com.google.common.base.Charsets} instead.
*/
public static final String UTF8_NAME = "UTF-8";
@Deprecated public static final String UTF8_NAME = "UTF-8";
/** The name of the ISO-8859-1 charset. */
public static final String ISO88591_NAME = "ISO-8859-1";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets} or {@link
* com.google.common.base.Charsets} instead.
*/
@Deprecated public static final String ISO88591_NAME = "ISO-8859-1";
/** The name of the UTF-16 charset. */
public static final String UTF16_NAME = "UTF-16";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets} or {@link
* com.google.common.base.Charsets} instead.
*/
@Deprecated public static final String UTF16_NAME = "UTF-16";
/** The name of the UTF-16 little-endian charset. */
public static final String UTF16LE_NAME = "UTF-16LE";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets} or {@link
* com.google.common.base.Charsets} instead.
*/
@Deprecated public static final String UTF16LE_NAME = "UTF-16LE";
/**
* The name of the serif font family.

View File

@ -15,7 +15,7 @@
*/
package com.google.android.exoplayer2.util;
import com.google.android.exoplayer2.C;
import com.google.common.base.Charsets;
import java.nio.charset.Charset;
/**
@ -288,7 +288,7 @@ public final class ParsableBitArray {
* @return The string encoded by the bytes in UTF-8.
*/
public String readBytesAsString(int length) {
return readBytesAsString(length, Charset.forName(C.UTF8_NAME));
return readBytesAsString(length, Charsets.UTF_8);
}
/**

View File

@ -16,7 +16,7 @@
package com.google.android.exoplayer2.util;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
@ -449,7 +449,7 @@ public final class ParsableByteArray {
* @return The string encoded by the bytes.
*/
public String readString(int length) {
return readString(length, Charset.forName(C.UTF8_NAME));
return readString(length, Charsets.UTF_8);
}
/**

View File

@ -54,6 +54,7 @@ import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.common.base.Ascii;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
@ -63,7 +64,6 @@ import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
@ -595,7 +595,7 @@ public final class Util {
* @return The string.
*/
public static String fromUtf8Bytes(byte[] bytes) {
return new String(bytes, Charset.forName(C.UTF8_NAME));
return new String(bytes, Charsets.UTF_8);
}
/**
@ -607,7 +607,7 @@ public final class Util {
* @return The string.
*/
public static String fromUtf8Bytes(byte[] bytes, int offset, int length) {
return new String(bytes, offset, length, Charset.forName(C.UTF8_NAME));
return new String(bytes, offset, length, Charsets.UTF_8);
}
/**
@ -617,7 +617,7 @@ public final class Util {
* @return The code points encoding using UTF-8.
*/
public static byte[] getUtf8Bytes(String value) {
return value.getBytes(Charset.forName(C.UTF8_NAME));
return value.getBytes(Charsets.UTF_8);
}
/**

View File

@ -21,11 +21,10 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -287,7 +286,7 @@ public final class Id3DecoderTest {
for (FrameSpec frame : frames) {
byte[] frameData = frame.frameData;
String frameId = frame.frameId;
byte[] frameIdBytes = frameId.getBytes(Charset.forName(C.UTF8_NAME));
byte[] frameIdBytes = frameId.getBytes(Charsets.UTF_8);
Assertions.checkState(frameIdBytes.length == 4);
// Fill in the frame header.

View File

@ -19,9 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -281,7 +280,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBytesAsStringDefaultsToUtf8() {
byte[] testData = "a non-åscii strìng".getBytes(Charset.forName(C.UTF8_NAME));
byte[] testData = "a non-åscii strìng".getBytes(Charsets.UTF_8);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBytes(2);
@ -290,18 +289,18 @@ public final class ParsableBitArrayTest {
@Test
public void readBytesAsStringExplicitCharset() {
byte[] testData = "a non-åscii strìng".getBytes(Charset.forName(C.UTF16_NAME));
byte[] testData = "a non-åscii strìng".getBytes(Charsets.UTF_16);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBytes(6);
assertThat(testArray.readBytesAsString(testData.length - 6, Charset.forName(C.UTF16_NAME)))
assertThat(testArray.readBytesAsString(testData.length - 6, Charsets.UTF_16))
.isEqualTo("non-åscii strìng");
}
@Test
public void readBytesNotByteAligned() {
String testString = "test string";
byte[] testData = testString.getBytes(Charset.forName(C.UTF8_NAME));
byte[] testData = testString.getBytes(Charsets.UTF_8);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBit();

View File

@ -34,9 +34,9 @@ import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -438,7 +438,7 @@ public final class FrameworkMediaDrm implements ExoMediaDrm {
return data;
}
int recordLength = byteArray.readLittleEndianShort();
String xml = byteArray.readString(recordLength, Charset.forName(C.UTF16LE_NAME));
String xml = byteArray.readString(recordLength, Charsets.UTF_16LE);
if (xml.contains("<LA_URL>")) {
// LA_URL already present. Do nothing.
return data;
@ -459,7 +459,7 @@ public final class FrameworkMediaDrm implements ExoMediaDrm {
newData.putShort((short) objectRecordCount);
newData.putShort((short) recordType);
newData.putShort((short) (xmlWithMockLaUrl.length() * UTF_16_BYTES_PER_CHARACTER));
newData.put(xmlWithMockLaUrl.getBytes(Charset.forName(C.UTF16LE_NAME)));
newData.put(xmlWithMockLaUrl.getBytes(Charsets.UTF_16LE));
return newData.array();
}
}

View File

@ -16,14 +16,13 @@
package com.google.android.exoplayer2.metadata.dvbsi;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataDecoder;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.ParsableBitArray;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
/**
@ -109,7 +108,7 @@ public final class AppInfoTableDecoder implements MetadataDecoder {
// See section 5.3.6.2.
while (sectionData.getBytePosition() < positionOfNextDescriptor) {
int urlBaseLength = sectionData.readBits(8);
urlBase = sectionData.readBytesAsString(urlBaseLength, Charset.forName(C.ASCII_NAME));
urlBase = sectionData.readBytesAsString(urlBaseLength, Charsets.US_ASCII);
int extensionCount = sectionData.readBits(8);
for (int urlExtensionIndex = 0;
@ -122,8 +121,7 @@ public final class AppInfoTableDecoder implements MetadataDecoder {
}
} else if (descriptorTag == DESCRIPTOR_SIMPLE_APPLICATION_LOCATION) {
// See section 5.3.7.
urlExtension =
sectionData.readBytesAsString(descriptorLength, Charset.forName(C.ASCII_NAME));
urlExtension = sectionData.readBytesAsString(descriptorLength, Charsets.US_ASCII);
}
sectionData.setPosition(positionOfNextDescriptor * 8);

View File

@ -16,15 +16,14 @@
package com.google.android.exoplayer2.metadata.icy;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataDecoder;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -40,8 +39,8 @@ public final class IcyDecoder implements MetadataDecoder {
private final CharsetDecoder iso88591Decoder;
public IcyDecoder() {
utf8Decoder = Charset.forName(C.UTF8_NAME).newDecoder();
iso88591Decoder = Charset.forName(C.ISO88591_NAME).newDecoder();
utf8Decoder = Charsets.UTF_8.newDecoder();
iso88591Decoder = Charsets.ISO_8859_1.newDecoder();
}
@Override

View File

@ -30,7 +30,7 @@ import com.google.android.exoplayer2.text.Subtitle;
import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import java.util.List;
/**
@ -171,10 +171,10 @@ public final class Tx3gDecoder extends SimpleSubtitleDecoder {
if (parsableByteArray.bytesLeft() >= SIZE_BOM_UTF16) {
char firstChar = parsableByteArray.peekChar();
if (firstChar == BOM_UTF16_BE || firstChar == BOM_UTF16_LE) {
return parsableByteArray.readString(textLength, Charset.forName(C.UTF16_NAME));
return parsableByteArray.readString(textLength, Charsets.UTF_16);
}
}
return parsableByteArray.readString(textLength, Charset.forName(C.UTF8_NAME));
return parsableByteArray.readString(textLength, Charsets.UTF_8);
}
private void applyStyleRecord(ParsableByteArray parsableByteArray,

View File

@ -23,6 +23,7 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.net.URLDecoder;
@ -63,7 +64,7 @@ public final class DataSchemeDataSource extends BaseDataSource {
}
} else {
// TODO: Add support for other charsets.
data = Util.getUtf8Bytes(URLDecoder.decode(dataString, C.ASCII_NAME));
data = Util.getUtf8Bytes(URLDecoder.decode(dataString, Charsets.US_ASCII.name()));
}
endPosition =
dataSpec.length != C.LENGTH_UNSET ? (int) dataSpec.length + readPosition : data.length;

View File

@ -16,9 +16,8 @@
package com.google.android.exoplayer2.upstream.cache;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -80,7 +79,7 @@ public final class DefaultContentMetadata implements ContentMetadata {
public final String get(String name, @Nullable String defaultValue) {
@Nullable byte[] bytes = metadata.get(name);
if (bytes != null) {
return new String(bytes, Charset.forName(C.UTF8_NAME));
return new String(bytes, Charsets.UTF_8);
} else {
return defaultValue;
}
@ -162,7 +161,7 @@ public final class DefaultContentMetadata implements ContentMetadata {
if (value instanceof Long) {
return ByteBuffer.allocate(8).putLong((Long) value).array();
} else if (value instanceof String) {
return ((String) value).getBytes(Charset.forName(C.UTF8_NAME));
return ((String) value).getBytes(Charsets.UTF_8);
} else if (value instanceof byte[]) {
return (byte[]) value;
} else {

View File

@ -63,11 +63,11 @@ import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.SntpClient;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Charsets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
@ -1451,8 +1451,7 @@ public final class DashMediaSource extends BaseMediaSource {
@Override
public Long parse(Uri uri, InputStream inputStream) throws IOException {
String firstLine =
new BufferedReader(new InputStreamReader(inputStream, Charset.forName(C.UTF8_NAME)))
.readLine();
new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8)).readLine();
try {
Matcher matcher = TIMESTAMP_WITH_TIMEZONE_PATTERN.matcher(firstLine);
if (!matcher.matches()) {

View File

@ -39,6 +39,7 @@ import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.UriUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.util.XmlPullParserUtil;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -933,7 +934,7 @@ public class DashManifestParser extends DefaultHandler
throws XmlPullParserException, IOException {
scratchOutputStream.reset();
XmlSerializer xmlSerializer = Xml.newSerializer();
xmlSerializer.setOutput(scratchOutputStream, C.UTF8_NAME);
xmlSerializer.setOutput(scratchOutputStream, Charsets.UTF_8.name());
// Start reading everything between <Event> and </Event>, and serialize them into an Xml
// byte array.
xpp.nextToken();

View File

@ -28,9 +28,9 @@ import com.google.android.exoplayer2.source.dash.manifest.SegmentBase.SegmentTim
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
@ -116,11 +116,7 @@ public class DashManifestParserTest {
assertThat(eventStream1.events.length).isEqualTo(1);
EventMessage expectedEvent1 =
new EventMessage(
"urn:uuid:XYZY",
"call",
10000,
0,
"+ 1 800 10101010".getBytes(Charset.forName(C.UTF8_NAME)));
"urn:uuid:XYZY", "call", 10000, 0, "+ 1 800 10101010".getBytes(Charsets.UTF_8));
assertThat(eventStream1.events[0]).isEqualTo(expectedEvent1);
assertThat(eventStream1.presentationTimesUs[0]).isEqualTo(0);

View File

@ -16,8 +16,7 @@
package com.google.android.exoplayer2.source.dash.offline;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
/** Data for DASH downloading tests. */
/* package */ interface DashDownloadTestData {
@ -87,7 +86,7 @@ import java.nio.charset.Charset;
+ " </AdaptationSet>\n"
+ " </Period>\n"
+ "</MPD>")
.getBytes(Charset.forName(C.UTF8_NAME));
.getBytes(Charsets.UTF_8);
byte[] TEST_MPD_NO_INDEX =
("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@ -100,5 +99,5 @@ import java.nio.charset.Charset;
+ " </AdaptationSet>\n"
+ " </Period>\n"
+ "</MPD>")
.getBytes(Charset.forName(C.UTF8_NAME));
.getBytes(Charsets.UTF_8);
}

View File

@ -16,7 +16,6 @@
package com.google.android.exoplayer2.extractor;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.VorbisUtil.CommentHeader;
import com.google.android.exoplayer2.metadata.Metadata;
@ -25,8 +24,8 @@ import com.google.android.exoplayer2.metadata.id3.Id3Decoder;
import com.google.android.exoplayer2.util.FlacConstants;
import com.google.android.exoplayer2.util.ParsableBitArray;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -275,7 +274,7 @@ public final class FlacMetadataReader {
int pictureType = scratch.readInt();
int mimeTypeLength = scratch.readInt();
String mimeType = scratch.readString(mimeTypeLength, Charset.forName(C.ASCII_NAME));
String mimeType = scratch.readString(mimeTypeLength, Charsets.US_ASCII);
int descriptionLength = scratch.readInt();
String description = scratch.readString(descriptionLength);
int width = scratch.readInt();

View File

@ -15,8 +15,7 @@
*/
package com.google.android.exoplayer2.source.hls.offline;
import com.google.android.exoplayer2.C;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
/** Data for HLS downloading tests. */
/* package */ interface HlsDownloadTestData {
@ -49,7 +48,7 @@ import java.nio.charset.Charset;
+ "\n"
+ "#EXT-X-STREAM-INF:BANDWIDTH=41457,CODECS=\"mp4a.40.2\"\n"
+ MEDIA_PLAYLIST_0_URI)
.getBytes(Charset.forName(C.UTF8_NAME));
.getBytes(Charsets.UTF_8);
byte[] MEDIA_PLAYLIST_DATA =
("#EXTM3U\n"
@ -64,7 +63,7 @@ import java.nio.charset.Charset;
+ "#EXTINF:9.97667,\n"
+ "fileSequence2.ts\n"
+ "#EXT-X-ENDLIST")
.getBytes(Charset.forName(C.UTF8_NAME));
.getBytes(Charsets.UTF_8);
String ENC_MEDIA_PLAYLIST_URI = "enc_index.m3u8";
@ -83,5 +82,5 @@ import java.nio.charset.Charset;
+ "#EXTINF:9.97667,\n"
+ "fileSequence2.ts\n"
+ "#EXT-X-ENDLIST")
.getBytes(Charset.forName(C.UTF8_NAME));
.getBytes(Charsets.UTF_8);
}

View File

@ -27,9 +27,9 @@ import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.source.hls.HlsTrackMetadataEntry;
import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist.Variant;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.base.Charsets;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -461,7 +461,7 @@ public class HlsMasterPlaylistParserTest {
throws IOException {
Uri playlistUri = Uri.parse(uri);
ByteArrayInputStream inputStream =
new ByteArrayInputStream(playlistString.getBytes(Charset.forName(C.UTF8_NAME)));
new ByteArrayInputStream(playlistString.getBytes(Charsets.UTF_8));
return (HlsMasterPlaylist) new HlsPlaylistParser().parse(playlistUri, inputStream);
}
}

View File

@ -41,6 +41,7 @@ dependencies {
api 'androidx.media:media:' + androidxMediaVersion
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
implementation 'androidx.appcompat:appcompat:' + androidxAppCompatVersion
implementation 'com.google.guava:guava:' + guavaVersion
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion
testImplementation project(modulePrefix + 'testutils')

View File

@ -29,11 +29,10 @@ import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.text.CaptionStyleCompat;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.util.Util;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -288,8 +287,7 @@ import java.util.List;
html.append("</div></body></html>");
webView.loadData(
Base64.encodeToString(
html.toString().getBytes(Charset.forName(C.UTF8_NAME)), Base64.NO_PADDING),
Base64.encodeToString(html.toString().getBytes(Charsets.UTF_8), Base64.NO_PADDING),
"text/html",
"base64");
}