Skip to content

Commit

Permalink
Use StandardCharsets.
Browse files Browse the repository at this point in the history
Better to skip some charset creations.

Change-Id: I31486cc44e1d8464bdf402d3aac21753bd5c8c26
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
  • Loading branch information
akurtakov committed Nov 27, 2019
1 parent 4e10248 commit 2cbc230
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2018 The Eclipse Foundation and others.
* Copyright (c) 2010, 2019 The Eclipse Foundation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -20,6 +20,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -844,7 +845,7 @@ public void reportInstallError(IStatus result, Set<? extends INode> nodes, Set<S
}
parameters.add(new BasicNameValuePair("detailedMessage", resolutionDetails)); //$NON-NLS-1$
if (!parameters.isEmpty()) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8"); //$NON-NLS-1$
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8);
method.setEntity(entity);
client.execute(method);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2018 The Eclipse Foundation and others.
* Copyright (c) 2014, 2019 The Eclipse Foundation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -23,9 +23,9 @@
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -60,7 +60,7 @@ public <T> T unmarshal(InputStream in, Class<T> type, IProgressMonitor monitor)
ByteBuffer peekBuffer = peekResponseContent(bufferedInput);

// FIXME how can the charset be determined?
Reader reader = new InputStreamReader(bufferedInput, RemoteMarketplaceService.UTF_8);
Reader reader = new InputStreamReader(bufferedInput, StandardCharsets.UTF_8);
reader = new StripInvalidXMLCharsReader(reader);
try {
xmlReader.parse(new InputSource(reader));
Expand Down Expand Up @@ -120,7 +120,7 @@ private IStatus createContentError(ByteBuffer peekBuffer, String message, Throwa
private IStatus createContentInfo(ByteBuffer peekBuffer) {
try {
StringBuilder message = new StringBuilder("Received response begins with:\n\n"); //$NON-NLS-1$
CharsetDecoder decoder = Charset.forName("ASCII").newDecoder(); //$NON-NLS-1$
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
decoder.replaceWith("?"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RemoteMarketplaceService<T> {

public static final String API_URI_SUFFIX = "api/p"; //$NON-NLS-1$

protected static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$

private static final int RETRY_COUNT = 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -467,7 +468,7 @@ public void news() throws IOException, UnmarshalException {
@Test(expected = UnmarshalException.class)
public void invalidLeadingSpace() throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(4096);
buffer.put(" ".getBytes("UTF-8"));
buffer.put(" ".getBytes(StandardCharsets.UTF_8));
buffer = readResource("resources/catalogs.xml", buffer);
buffer.flip();
process(buffer);
Expand Down Expand Up @@ -588,7 +589,7 @@ private Object process(InputStream in) throws IOException, UnmarshalException {
}

private Object process(String content) throws IOException, UnmarshalException {
return process(content.getBytes("UTF-8"));
return process(content.getBytes(StandardCharsets.UTF_8));
}

private Object process(ByteBuffer buffer) throws IOException, UnmarshalException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@
*******************************************************************************/
package org.eclipse.epp.mpc.tests.util;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
Expand Down Expand Up @@ -255,7 +264,8 @@ public void testHttpClientTransportFallback() throws Exception {
httpClientFactory.setTransport(httpClientTransport);
ITransportFactory secondaryFactory = Mockito.mock(ITransportFactory.class);
ITransport secondaryTransport = Mockito.mock(ITransport.class);
InputStream expectedResultStream = new ByteArrayInputStream("Secondary transport".getBytes("UTF-8"));
InputStream expectedResultStream = new ByteArrayInputStream("Secondary transport".getBytes(
StandardCharsets.UTF_8));
Mockito.when(secondaryFactory.getTransport()).thenReturn(secondaryTransport);
Mockito.when(secondaryTransport.stream(ArgumentMatchers.<URI> any(), ArgumentMatchers.<IProgressMonitor> any())).thenReturn(
expectedResultStream);
Expand Down Expand Up @@ -381,7 +391,7 @@ private static String getTransportServiceName() {
BundleContext bundleContext = FrameworkUtil.getBundle(TransportFactory.class).getBundleContext();
Collection<ServiceReference<ITransportFactory>> transportServiceReferences = TransportFactory
.getTransportServiceReferences(
bundleContext);
bundleContext);
if (!transportServiceReferences.isEmpty()) {
ServiceReference<ITransportFactory> transportServiceReference = transportServiceReferences.iterator()
.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class BrowseCatalogItem extends UserActionViewerItem<CatalogDescriptor> {

private static final String TID = "tid:"; //$NON-NLS-1$

private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$

private final MarketplaceCategory category;

private final IMarketplaceWebBrowser browser;
Expand Down

0 comments on commit 2cbc230

Please sign in to comment.