Skip to content

Commit 81d03fc

Browse files
authored
Using Java7+ NIO API for improved performance (#5350)
1 parent 8cee8f4 commit 81d03fc

File tree

27 files changed

+125
-122
lines changed

27 files changed

+125
-122
lines changed

core-common/src/main/java/org/glassfish/jersey/SslConfigurator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,10 +17,11 @@
1717
package org.glassfish.jersey;
1818

1919
import java.io.ByteArrayInputStream;
20-
import java.io.FileInputStream;
2120
import java.io.FileNotFoundException;
2221
import java.io.IOException;
2322
import java.io.InputStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.Paths;
2425
import java.security.AccessController;
2526
import java.security.KeyManagementException;
2627
import java.security.KeyStore;
@@ -635,7 +636,7 @@ public SSLContext createSSLContext() {
635636
if (keyStoreBytes != null) {
636637
keyStoreInputStream = new ByteArrayInputStream(keyStoreBytes);
637638
} else if (!keyStoreFile.equals("NONE")) {
638-
keyStoreInputStream = new FileInputStream(keyStoreFile);
639+
keyStoreInputStream = Files.newInputStream(Paths.get(keyStoreFile));
639640
}
640641
_keyStore.load(keyStoreInputStream, keyStorePass);
641642
} finally {
@@ -710,7 +711,7 @@ public SSLContext createSSLContext() {
710711
if (trustStoreBytes != null) {
711712
trustStoreInputStream = new ByteArrayInputStream(trustStoreBytes);
712713
} else if (!trustStoreFile.equals("NONE")) {
713-
trustStoreInputStream = new FileInputStream(trustStoreFile);
714+
trustStoreInputStream = Files.newInputStream(Paths.get(trustStoreFile));
714715
}
715716
_trustStore.load(trustStoreInputStream, trustStorePass);
716717
} finally {

core-common/src/main/java/org/glassfish/jersey/message/internal/AbstractMessageReaderWriterProvider.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -24,6 +24,7 @@
2424
import java.lang.annotation.Annotation;
2525
import java.lang.reflect.Type;
2626
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2728

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

4344
/**
4445
* The UTF-8 Charset.
46+
*
47+
* @deprecated use {@code StandardCharsets.UTF_8} instead.
4548
*/
46-
public static final Charset UTF8 = ReaderWriter.UTF8;
49+
@Deprecated
50+
public static final Charset UTF8 = StandardCharsets.UTF_8;
4751

4852
/**
4953
* Reader bytes from an input stream and write then to an output stream.
5054
*
5155
* @param in the input stream to read from.
5256
* @param out the output stream to write to.
5357
* @throws IOException if there is an error reading or writing bytes.
58+
*
59+
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
5460
*/
61+
@Deprecated
5562
public static void writeTo(InputStream in, OutputStream out) throws IOException {
5663
ReaderWriter.writeTo(in, out);
5764
}
@@ -62,7 +69,10 @@ public static void writeTo(InputStream in, OutputStream out) throws IOException
6269
* @param in the reader to read from.
6370
* @param out the writer to write to.
6471
* @throws IOException if there is an error reading or writing characters.
72+
*
73+
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
6574
*/
75+
@Deprecated
6676
public static void writeTo(Reader in, Writer out) throws IOException {
6777
ReaderWriter.writeTo(in, out);
6878
}

core-common/src/main/java/org/glassfish/jersey/message/internal/FileProvider.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,16 +16,14 @@
1616

1717
package org.glassfish.jersey.message.internal;
1818

19-
import java.io.BufferedInputStream;
20-
import java.io.BufferedOutputStream;
2119
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.FileOutputStream;
2420
import java.io.IOException;
2521
import java.io.InputStream;
2622
import java.io.OutputStream;
2723
import java.lang.annotation.Annotation;
2824
import java.lang.reflect.Type;
25+
import java.nio.file.Files;
26+
import java.nio.file.StandardCopyOption;
2927

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

67-
try {
68-
writeTo(entityStream, stream);
69-
} finally {
70-
stream.close();
71-
}
64+
Files.copy(entityStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
7265

7366
return file;
7467
}
@@ -89,13 +82,7 @@ public void writeTo(final File t,
8982
final MediaType mediaType,
9083
final MultivaluedMap<String, Object> httpHeaders,
9184
final OutputStream entityStream) throws IOException {
92-
final InputStream stream = new BufferedInputStream(new FileInputStream(t), ReaderWriter.BUFFER_SIZE);
93-
94-
try {
95-
writeTo(stream, entityStream);
96-
} finally {
97-
stream.close();
98-
}
85+
Files.copy(t.toPath(), entityStream);
9986
}
10087

10188
@Override

core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import java.io.Closeable;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22-
import java.io.InputStreamReader;
2322
import java.io.OutputStream;
2423
import java.io.OutputStreamWriter;
2524
import java.io.Reader;
2625
import java.io.Writer;
2726
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2828
import java.security.AccessController;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
@@ -54,8 +54,11 @@ public final class ReaderWriter {
5454
private static final Logger LOGGER = Logger.getLogger(ReaderWriter.class.getName());
5555
/**
5656
* The UTF-8 Charset.
57+
*
58+
* @deprecated use {@code StandardCharsets.UTF_8} instead
5759
*/
58-
public static final Charset UTF8 = Charset.forName("UTF-8");
60+
@Deprecated
61+
public static final Charset UTF8 = StandardCharsets.UTF_8;
5962
/**
6063
* The buffer size for arrays of byte and character.
6164
*/
@@ -167,7 +170,7 @@ public static String readFromAsString(Reader reader) throws IOException {
167170

168171
/**
169172
* Java 9+ InputStream::readAllBytes
170-
* TODO Replace when Java 8 not any longer supported (3.1+)
173+
* TODO Replace in Jersey 4.0, as the sole difference to OpenJDK is working around a bug in the input stream.
171174
*/
172175
private static byte[] readAllBytes(InputStream inputStream) throws IOException {
173176
List<byte[]> bufs = null;
@@ -243,7 +246,7 @@ private static byte[] readAllBytes(InputStream inputStream) throws IOException {
243246
*/
244247
public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException {
245248
Writer osw = new OutputStreamWriter(out, getCharset(type));
246-
osw.write(s, 0, s.length());
249+
osw.write(s);
247250
osw.flush();
248251
}
249252

core-common/src/test/java/org/glassfish/jersey/message/internal/UtilsTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -22,22 +22,19 @@
2222
import java.io.BufferedOutputStream;
2323
import java.io.ByteArrayInputStream;
2424
import java.io.File;
25-
import java.io.FileOutputStream;
2625
import java.io.IOException;
2726
import java.io.OutputStream;
27+
import java.nio.file.Files;
2828

2929
public class UtilsTest {
3030

3131
@Test
3232
public void createTempFile() throws IOException {
3333
final File file = Utils.createTempFile();
34-
final OutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
3534

36-
try {
35+
try (final OutputStream stream = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
3736
final ByteArrayInputStream entityStream = new ByteArrayInputStream("Test stream byte input".getBytes());
3837
ReaderWriter.writeTo(entityStream, stream);
39-
} finally {
40-
stream.close();
4138
}
4239
Assertions.assertTrue(file.exists());
4340
}

core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/FileSchemeResourceFinderFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,10 +17,10 @@
1717
package org.glassfish.jersey.server.internal.scanning;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
21-
import java.io.FileNotFoundException;
2220
import java.io.InputStream;
21+
import java.io.IOException;
2322
import java.net.URI;
23+
import java.nio.file.Files;
2424
import java.util.Collections;
2525
import java.util.NoSuchElementException;
2626
import java.util.Set;
@@ -140,8 +140,8 @@ public String next() {
140140
@Override
141141
public InputStream open() {
142142
try {
143-
return new FileInputStream(current);
144-
} catch (final FileNotFoundException e) {
143+
return Files.newInputStream(current.toPath());
144+
} catch (final IOException e) {
145145
throw new ResourceFinderException(e);
146146
}
147147
}

core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/FilesScanner.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,10 +17,9 @@
1717
package org.glassfish.jersey.server.internal.scanning;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
21-
import java.io.FileNotFoundException;
2220
import java.io.IOException;
2321
import java.io.InputStream;
22+
import java.nio.file.Files;
2423
import java.util.NoSuchElementException;
2524
import java.util.Stack;
2625

@@ -60,7 +59,7 @@ public FilesScanner(final String[] fileNames, final boolean recursive) {
6059
private void processFile(final File f) {
6160
if (f.getName().endsWith(".jar") || f.getName().endsWith(".zip")) {
6261
try {
63-
compositeResourceFinder.push(new JarFileScanner(new FileInputStream(f), "", true));
62+
compositeResourceFinder.push(new JarFileScanner(Files.newInputStream(f.toPath()), "", true));
6463
} catch (final IOException e) {
6564
// logging might be sufficient in this case
6665
throw new ResourceFinderException(e);
@@ -117,8 +116,8 @@ public String next() {
117116
@Override
118117
public InputStream open() {
119118
try {
120-
return new FileInputStream(current);
121-
} catch (final FileNotFoundException e) {
119+
return Files.newInputStream(current.toPath());
120+
} catch (final IOException e) {
122121
throw new ResourceFinderException(e);
123122
}
124123
}

core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/JarZipSchemeResourceFinderFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,12 +16,13 @@
1616

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

19-
import java.io.FileInputStream;
2019
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.net.MalformedURLException;
2322
import java.net.URI;
2423
import java.net.URL;
24+
import java.nio.file.Files;
25+
import java.nio.file.Paths;
2526
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.HashSet;
@@ -140,7 +141,7 @@ public void reset() {
140141
* if that fails with a {@link java.net.MalformedURLException} then the method will
141142
* attempt to create a {@link InputStream} instance as follows:
142143
* <pre>
143-
* return new new FileInputStream(
144+
* return Files.newInputStream(
144145
* UriComponent.decode(jarUrlString, UriComponent.Type.PATH)));
145146
* </pre>
146147
*
@@ -153,8 +154,8 @@ private InputStream getInputStream(final String jarUrlString) throws IOException
153154
try {
154155
return new URL(jarUrlString).openStream();
155156
} catch (final MalformedURLException e) {
156-
return new FileInputStream(
157-
UriComponent.decode(jarUrlString, UriComponent.Type.PATH));
157+
return Files.newInputStream(
158+
Paths.get(UriComponent.decode(jarUrlString, UriComponent.Type.PATH)));
158159
}
159160
}
160161
}

core-server/src/main/java/org/glassfish/jersey/server/wadl/internal/generators/WadlGeneratorApplicationDoc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,8 +17,8 @@
1717
package org.glassfish.jersey.server.wadl.internal.generators;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.InputStream;
21+
import java.nio.file.Files;
2222
import java.util.List;
2323

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

104104
InputStream inputStream;
105105
if (_applicationDocsFile != null) {
106-
inputStream = new FileInputStream(_applicationDocsFile);
106+
inputStream = Files.newInputStream(_applicationDocsFile.toPath());
107107
} else {
108108
inputStream = _applicationDocsStream;
109109
}

core-server/src/main/java/org/glassfish/jersey/server/wadl/internal/generators/WadlGeneratorGrammarsSupport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,8 +17,8 @@
1717
package org.glassfish.jersey.server.wadl.internal.generators;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.InputStream;
21+
import java.nio.file.Files;
2222
import java.util.List;
2323
import java.util.logging.Logger;
2424

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

0 commit comments

Comments
 (0)