Skip to content

Commit

Permalink
Using Java7+ NIO API for improved performance: Modern JREs offload to…
Browse files Browse the repository at this point in the history
… OS, and do other tricks.
  • Loading branch information
mkarg committed Jun 18, 2023
1 parent d60da24 commit ff89730
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,10 +17,11 @@
package org.glassfish.jersey;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.KeyManagementException;
import java.security.KeyStore;
Expand Down Expand Up @@ -635,7 +636,7 @@ public SSLContext createSSLContext() {
if (keyStoreBytes != null) {
keyStoreInputStream = new ByteArrayInputStream(keyStoreBytes);
} else if (!keyStoreFile.equals("NONE")) {
keyStoreInputStream = new FileInputStream(keyStoreFile);
keyStoreInputStream = Files.newInputStream(Paths.get(keyStoreFile));
}
_keyStore.load(keyStoreInputStream, keyStorePass);
} finally {
Expand Down Expand Up @@ -710,7 +711,7 @@ public SSLContext createSSLContext() {
if (trustStoreBytes != null) {
trustStoreInputStream = new ByteArrayInputStream(trustStoreBytes);
} else if (!trustStoreFile.equals("NONE")) {
trustStoreInputStream = new FileInputStream(trustStoreFile);
trustStoreInputStream = Files.newInputStream(Paths.get(trustStoreFile));
}
_trustStore.load(trustStoreInputStream, trustStorePass);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,6 +24,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.MessageBodyReader;
Expand All @@ -42,16 +43,22 @@ public abstract class AbstractMessageReaderWriterProvider<T> implements MessageB

/**
* The UTF-8 Charset.
*
* @deprecated use {@code StandardCharsets.UTF_8} instead.
*/
public static final Charset UTF8 = ReaderWriter.UTF8;
@Deprecated(forRemoval = true)
public static final Charset UTF8 = StandardCharsets.UTF_8;

/**
* Reader bytes from an input stream and write then to an output stream.
*
* @param in the input stream to read from.
* @param out the output stream to write to.
* @throws IOException if there is an error reading or writing bytes.
*
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
*/
@Deprecated
public static void writeTo(InputStream in, OutputStream out) throws IOException {
ReaderWriter.writeTo(in, out);
}
Expand All @@ -62,7 +69,10 @@ public static void writeTo(InputStream in, OutputStream out) throws IOException
* @param in the reader to read from.
* @param out the writer to write to.
* @throws IOException if there is an error reading or writing characters.
*
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
*/
@Deprecated
public static void writeTo(Reader in, Writer out) throws IOException {
ReaderWriter.writeTo(in, out);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,16 +16,14 @@

package org.glassfish.jersey.message.internal;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
Expand Down Expand Up @@ -62,13 +60,8 @@ public File readFrom(final Class<File> type,
final MultivaluedMap<String, String> httpHeaders,
final InputStream entityStream) throws IOException {
final File file = Utils.createTempFile();
final OutputStream stream = new BufferedOutputStream(new FileOutputStream(file));

try {
writeTo(entityStream, stream);
} finally {
stream.close();
}
Files.copy(entityStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);

return file;
}
Expand All @@ -89,13 +82,7 @@ public void writeTo(final File t,
final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream entityStream) throws IOException {
final InputStream stream = new BufferedInputStream(new FileInputStream(t), ReaderWriter.BUFFER_SIZE);

try {
writeTo(stream, entityStream);
} finally {
stream.close();
}
Files.copy(t.toPath(), entityStream);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -54,8 +54,11 @@ public final class ReaderWriter {
private static final Logger LOGGER = Logger.getLogger(ReaderWriter.class.getName());
/**
* The UTF-8 Charset.
*
* @deprecated use {@code StandardCharsets.UTF_8} instead
*/
public static final Charset UTF8 = Charset.forName("UTF-8");
@Deprecated(forRemoval = true)
public static final Charset UTF8 = StandardCharsets.UTF_8;
/**
* The buffer size for arrays of byte and character.
*/
Expand Down Expand Up @@ -167,7 +170,7 @@ public static String readFromAsString(Reader reader) throws IOException {

/**
* Java 9+ InputStream::readAllBytes
* TODO Replace when Java 8 not any longer supported (3.1+)
* TODO Replace in Jersey 4.0, as the sole difference to OpenJDK is working around a bug in the input stream.
*/
private static byte[] readAllBytes(InputStream inputStream) throws IOException {
List<byte[]> bufs = null;
Expand Down Expand Up @@ -242,9 +245,9 @@ private static byte[] readAllBytes(InputStream inputStream) throws IOException {
* @throws IOException in case of a write failure.
*/
public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException {
Writer osw = new OutputStreamWriter(out, getCharset(type));
osw.write(s, 0, s.length());
osw.flush();
try (final Writer osw = new OutputStreamWriter(out, getCharset(type))) {
osw.write(s);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -22,22 +22,19 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;

public class UtilsTest {

@Test
public void createTempFile() throws IOException {
final File file = Utils.createTempFile();
final OutputStream stream = new BufferedOutputStream(new FileOutputStream(file));

try {
try (final OutputStream stream = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
final ByteArrayInputStream entityStream = new ByteArrayInputStream("Test stream byte input".getBytes());
ReaderWriter.writeTo(entityStream, stream);
} finally {
stream.close();
}
Assertions.assertTrue(file.exists());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,10 +17,10 @@
package org.glassfish.jersey.server.internal.scanning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Set;
Expand Down Expand Up @@ -140,8 +140,8 @@ public String next() {
@Override
public InputStream open() {
try {
return new FileInputStream(current);
} catch (final FileNotFoundException e) {
return Files.newInputStream(current.toPath());
} catch (final IOException e) {
throw new ResourceFinderException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,10 +17,9 @@
package org.glassfish.jersey.server.internal.scanning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.NoSuchElementException;
import java.util.Stack;

Expand Down Expand Up @@ -60,7 +59,7 @@ public FilesScanner(final String[] fileNames, final boolean recursive) {
private void processFile(final File f) {
if (f.getName().endsWith(".jar") || f.getName().endsWith(".zip")) {
try {
compositeResourceFinder.push(new JarFileScanner(new FileInputStream(f), "", true));
compositeResourceFinder.push(new JarFileScanner(Files.newInputStream(f.toPath()), "", true));
} catch (final IOException e) {
// logging might be sufficient in this case
throw new ResourceFinderException(e);
Expand Down Expand Up @@ -117,8 +116,8 @@ public String next() {
@Override
public InputStream open() {
try {
return new FileInputStream(current);
} catch (final FileNotFoundException e) {
return Files.newInputStream(current.toPath());
} catch (final IOException e) {
throw new ResourceFinderException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,12 +16,13 @@

package org.glassfish.jersey.server.internal.scanning;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void reset() {
* if that fails with a {@link java.net.MalformedURLException} then the method will
* attempt to create a {@link InputStream} instance as follows:
* <pre>
* return new new FileInputStream(
* return Files.newInputStream(
* UriComponent.decode(jarUrlString, UriComponent.Type.PATH)));
* </pre>
*
Expand All @@ -153,8 +154,8 @@ private InputStream getInputStream(final String jarUrlString) throws IOException
try {
return new URL(jarUrlString).openStream();
} catch (final MalformedURLException e) {
return new FileInputStream(
UriComponent.decode(jarUrlString, UriComponent.Type.PATH));
return Files.newInputStream(
Paths.get(UriComponent.decode(jarUrlString, UriComponent.Type.PATH)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,8 +17,8 @@
package org.glassfish.jersey.server.wadl.internal.generators;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;

import javax.ws.rs.core.Context;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void init() throws Exception {

InputStream inputStream;
if (_applicationDocsFile != null) {
inputStream = new FileInputStream(_applicationDocsFile);
inputStream = Files.newInputStream(_applicationDocsFile.toPath());
} else {
inputStream = _applicationDocsStream;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,8 +17,8 @@
package org.glassfish.jersey.server.wadl.internal.generators;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.logging.Logger;

Expand Down Expand Up @@ -116,7 +116,7 @@ public void init() throws Exception {
+ " is set, one of both is required.");
}
_delegate.init();
_grammars = WadlUtils.unmarshall(_grammarsFile != null ? new FileInputStream(_grammarsFile) : _grammarsStream,
_grammars = WadlUtils.unmarshall(_grammarsFile != null ? Files.newInputStream(_grammarsFile.toPath()) : _grammarsStream,
saxFactoryProvider.get(), Grammars.class);
}

Expand Down
Loading

0 comments on commit ff89730

Please sign in to comment.