keep, un-deprecate and rename Resource.getReadableByteChannel() and Resource.getInputStream()

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2022-07-25 15:28:42 +02:00
parent 2275e1c2c6
commit 4856a7d065
25 changed files with 95 additions and 76 deletions

View File

@ -103,7 +103,7 @@ public class DebugHandlerTest
secureServerURI = URI.create(String.format("https://%s:%d/", host, sslConnector.getLocalPort()));
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream stream = sslContextFactory.getKeyStoreResource().getInputStream())
try (InputStream stream = sslContextFactory.getKeyStoreResource().newInputStream())
{
keystore.load(stream, "storepwd".toCharArray());
}

View File

@ -31,7 +31,6 @@ import java.util.stream.Stream;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
@ -101,13 +100,15 @@ public class RangeWriterTest
return Stream.of(
Arguments.of("Traditional / Direct Buffer", new ByteBufferRangeWriter(BufferUtil.toBuffer(realFileSystemResource, true))),
Arguments.of("Traditional / Indirect Buffer", new ByteBufferRangeWriter(BufferUtil.toBuffer(realFileSystemResource, false))),
Arguments.of("Traditional / SeekableByteChannel", new SeekableByteChannelRangeWriter(() -> (SeekableByteChannel)realFileSystemResource.getReadableByteChannel())),
Arguments.of("Traditional / InputStream", new InputStreamRangeWriter(() -> realFileSystemResource.getInputStream())),
// TODO the cast to SeekableByteChannel is questionable
Arguments.of("Traditional / SeekableByteChannel", new SeekableByteChannelRangeWriter(() -> (SeekableByteChannel)realFileSystemResource.newReadableByteChannel())),
Arguments.of("Traditional / InputStream", new InputStreamRangeWriter(() -> realFileSystemResource.newInputStream())),
Arguments.of("Non-Default FS / Direct Buffer", new ByteBufferRangeWriter(BufferUtil.toBuffer(nonDefaultFileSystemResource, true))),
Arguments.of("Non-Default FS / Indirect Buffer", new ByteBufferRangeWriter(BufferUtil.toBuffer(nonDefaultFileSystemResource, false))),
Arguments.of("Non-Default FS / SeekableByteChannel", new SeekableByteChannelRangeWriter(() -> (SeekableByteChannel)nonDefaultFileSystemResource.getReadableByteChannel())),
Arguments.of("Non-Default FS / InputStream", new InputStreamRangeWriter(() -> nonDefaultFileSystemResource.getInputStream()))
// TODO the cast to SeekableByteChannel is questionable
Arguments.of("Non-Default FS / SeekableByteChannel", new SeekableByteChannelRangeWriter(() -> (SeekableByteChannel)nonDefaultFileSystemResource.newReadableByteChannel())),
Arguments.of("Non-Default FS / InputStream", new InputStreamRangeWriter(() -> nonDefaultFileSystemResource.newInputStream()))
);
}

View File

@ -85,7 +85,7 @@ public class ServerConnectorSslServerTest extends HttpServerTestBase
initServer(connector);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream stream = sslContextFactory.getKeyStoreResource().getInputStream())
try (InputStream stream = sslContextFactory.getKeyStoreResource().newInputStream())
{
keystore.load(stream, "storepwd".toCharArray());
}

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.util.List;
@ -30,6 +31,35 @@ public class EmptyResource extends Resource
{
public static final Resource INSTANCE = new EmptyResource();
private static final ReadableByteChannel EOF_READABLE_BYTE_CHANNEL = new ReadableByteChannel()
{
@Override
public int read(ByteBuffer dst)
{
return -1;
}
@Override
public boolean isOpen()
{
return false;
}
@Override
public void close()
{
}
};
private static final InputStream EOF_INPUT_STREAM = new InputStream()
{
@Override
public int read()
{
return -1;
}
};
private EmptyResource()
{
}
@ -83,15 +113,15 @@ public class EmptyResource extends Resource
}
@Override
public InputStream getInputStream() throws IOException
public InputStream newInputStream() throws IOException
{
return null;
return EOF_INPUT_STREAM;
}
@Override
public ReadableByteChannel getReadableByteChannel() throws IOException
public ReadableByteChannel newReadableByteChannel() throws IOException
{
return null;
return EOF_READABLE_BYTE_CHANNEL;
}
@Override

View File

@ -481,14 +481,12 @@ public abstract class Resource implements ResourceFactory
public abstract String getName();
/**
* Input stream to the resource
* Creates a new input stream to the resource.
*
* @return an input stream to the resource
* @throws IOException if unable to open the input stream
* @deprecated Replace with {@link #getPath()} and {@link Files#newInputStream(Path, OpenOption...)}.
*/
@Deprecated(forRemoval = true)
public InputStream getInputStream() throws IOException
public InputStream newInputStream() throws IOException
{
return Files.newInputStream(getPath(), StandardOpenOption.READ);
}
@ -498,10 +496,8 @@ public abstract class Resource implements ResourceFactory
*
* @return an readable bytechannel to the resource or null if one is not available.
* @throws IOException if unable to open the readable bytechannel for the resource.
* @deprecated Replace with {@link #getPath()} and {@link Files#newByteChannel(Path, OpenOption...)}.
*/
@Deprecated(forRemoval = true)
public ReadableByteChannel getReadableByteChannel() throws IOException
public ReadableByteChannel newReadableByteChannel() throws IOException
{
return Files.newByteChannel(getPath(), StandardOpenOption.READ);
}
@ -1047,7 +1043,7 @@ public abstract class Resource implements ResourceFactory
}
// use old school stream based copy
try (InputStream in = getInputStream();
try (InputStream in = newInputStream();
OutputStream out = Files.newOutputStream(destination))
{
IO.copy(in, out);

View File

@ -27,7 +27,6 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
@ -313,7 +312,7 @@ public class ResourceCollection extends Resource
}
@Override
public InputStream getInputStream() throws IOException
public InputStream newInputStream() throws IOException
{
assertResourcesSet();
@ -324,7 +323,7 @@ public class ResourceCollection extends Resource
// Skip, cannot open anyway
continue;
}
InputStream is = r.getInputStream();
InputStream is = r.newInputStream();
if (is != null)
{
return is;
@ -335,13 +334,13 @@ public class ResourceCollection extends Resource
}
@Override
public ReadableByteChannel getReadableByteChannel() throws IOException
public ReadableByteChannel newReadableByteChannel() throws IOException
{
assertResourcesSet();
for (Resource r : _resources)
{
ReadableByteChannel channel = r.getReadableByteChannel();
ReadableByteChannel channel = r.newReadableByteChannel();
if (channel != null)
{
return channel;

View File

@ -44,7 +44,7 @@ public class CertificateUtils
if (!store.exists())
throw new IllegalStateException(store.getName() + " is not a valid keystore");
try (InputStream inStream = store.getInputStream())
try (InputStream inStream = store.newInputStream())
{
keystore.load(inStream, storePassword == null ? null : storePassword.toCharArray());
}
@ -59,19 +59,10 @@ public class CertificateUtils
if (crlPath != null)
{
InputStream in = null;
try
try (InputStream in = Resource.newResource(crlPath).newInputStream())
{
in = Resource.newResource(crlPath).getInputStream();
crlList = CertificateFactory.getInstance("X.509").generateCRLs(in);
}
finally
{
if (in != null)
{
in.close();
}
}
}
return crlList;

View File

@ -432,7 +432,7 @@ public class FileSystemResourceTest
Resource base = Resource.newResource(dir);
Resource foo = base.resolve("foo");
try (InputStream stream = foo.getInputStream();
try (InputStream stream = foo.newInputStream();
InputStreamReader reader = new InputStreamReader(stream);
StringWriter writer = new StringWriter())
{
@ -458,7 +458,7 @@ public class FileSystemResourceTest
Resource base = Resource.newResource(dir);
Resource foo = base.resolve("foo");
try (ReadableByteChannel channel = foo.getReadableByteChannel())
try (ReadableByteChannel channel = foo.newReadableByteChannel())
{
ByteBuffer buf = ByteBuffer.allocate(256);
channel.read(buf);
@ -1290,7 +1290,7 @@ public class FileSystemResourceTest
private String toString(Resource resource) throws IOException
{
try (InputStream inputStream = resource.getInputStream();
try (InputStream inputStream = resource.newInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
{
IO.copy(inputStream, outputStream);

View File

@ -54,7 +54,7 @@ public class PathResourceTest
PathResource resource = (PathResource)Resource.newResource(manifestPath);
try (InputStream inputStream = resource.getInputStream())
try (InputStream inputStream = resource.newInputStream())
{
assertThat("InputStream", inputStream, is(not(nullValue())));
}
@ -78,7 +78,7 @@ public class PathResourceTest
PathResource resource = (PathResource)Resource.newResource(manifestPath);
try (ReadableByteChannel channel = resource.getReadableByteChannel())
try (ReadableByteChannel channel = resource.newReadableByteChannel())
{
assertThat("ReadableByteChannel", channel, is(not(nullValue())));
}

View File

@ -18,7 +18,6 @@ import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Arrays;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
@ -158,8 +157,8 @@ public class ResourceCollectionTest
assertThrows(IllegalStateException.class, () -> coll.resolve("foo"));
assertThrows(IllegalStateException.class, coll::exists);
assertThrows(IllegalStateException.class, coll::getPath);
assertThrows(IllegalStateException.class, coll::getInputStream);
assertThrows(IllegalStateException.class, coll::getReadableByteChannel);
assertThrows(IllegalStateException.class, coll::newInputStream);
assertThrows(IllegalStateException.class, coll::newReadableByteChannel);
assertThrows(IllegalStateException.class, coll::getURI);
assertThrows(IllegalStateException.class, coll::getName);
assertThrows(IllegalStateException.class, coll::isDirectory);
@ -253,7 +252,7 @@ public class ResourceCollectionTest
{
Resource resource = r.resolve(path);
StringBuilder buffer = new StringBuilder();
try (InputStream in = resource.getInputStream();
try (InputStream in = resource.newInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader))
{

View File

@ -291,7 +291,7 @@ public class ResourceTest
{
Assumptions.assumeTrue(data.content != null);
InputStream in = data.resource.getInputStream();
InputStream in = data.resource.newInputStream();
String c = IO.toString(in);
assertThat("Content: " + data.test, c, startsWith(data.content));
}

View File

@ -581,7 +581,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -607,7 +607,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(clazz.getName(), r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -656,7 +656,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -740,7 +740,7 @@ public class AnnotationParser
if (fullname.endsWith(".class"))
{
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
return;
@ -790,7 +790,7 @@ public class AnnotationParser
if (LOG.isDebugEnabled())
LOG.debug("Scanning class {}", r);
addParsedClass(str, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, Resource.newResource(path.getParent()), is);
}

View File

@ -3119,7 +3119,7 @@ public class ServletContextHandler extends ContextHandler implements Graceful
// Cannot serve directories as an InputStream
if (r.isDirectory())
return null;
return r.getInputStream();
return r.newInputStream();
}
catch (Exception e)
{

View File

@ -98,7 +98,7 @@ public class JDBCLoginService extends AbstractLoginService
{
Properties properties = new Properties();
Resource resource = Resource.newResource(_config);
try (InputStream in = resource.getInputStream())
try (InputStream in = resource.newInputStream())
{
properties.load(in);
}

View File

@ -225,7 +225,7 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
throw new IllegalStateException("Config does not exist: " + config);
Properties properties = new Properties();
try (InputStream inputStream = config.getInputStream())
try (InputStream inputStream = config.newInputStream())
{
properties.load(inputStream);
}

View File

@ -71,13 +71,13 @@ public class OrderingTest
}
@Override
public InputStream getInputStream() throws IOException
public InputStream newInputStream() throws IOException
{
return null;
}
@Override
public ReadableByteChannel getReadableByteChannel() throws IOException
public ReadableByteChannel newReadableByteChannel() throws IOException
{
return null;
}

View File

@ -83,14 +83,14 @@ public abstract class AbstractWebAppObjectInSessionTest extends AbstractSessionT
//File sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
File targetFile = new File(packageDirs, resourceName);
//copy(sourceFile, targetFile);
IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
IO.copy(resource.newInputStream(), new FileOutputStream(targetFile));
resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + "$" + WebAppObjectInSessionServlet.TestSharedStatic.class.getSimpleName() + ".class";
resource = Resource.newResource(getClass().getResource(resourceName));
//sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
targetFile = new File(packageDirs, resourceName);
//copy(sourceFile, targetFile);
IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
IO.copy(resource.newInputStream(), new FileOutputStream(targetFile));
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);

View File

@ -581,7 +581,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -607,7 +607,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(clazz.getName(), r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -656,7 +656,7 @@ public class AnnotationParser
{
Resource r = Resource.newResource(resource);
addParsedClass(className, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
}
@ -740,7 +740,7 @@ public class AnnotationParser
if (fullname.endsWith(".class"))
{
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, null, is);
return;
@ -790,7 +790,7 @@ public class AnnotationParser
if (LOG.isDebugEnabled())
LOG.debug("Scanning class {}", r);
addParsedClass(str, r.toString());
try (InputStream is = r.getInputStream())
try (InputStream is = r.newInputStream())
{
scanClass(handlers, Resource.newResource(path.getParent()), is);
}

View File

@ -1886,7 +1886,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
// Cannot serve directories as an InputStream
if (r.isDirectory())
return null;
return r.getInputStream();
return r.newInputStream();
}
catch (Exception e)
{

View File

@ -851,7 +851,7 @@ public class ResourceService
return;
}
try (InputStream input = content.getResource().getInputStream())
try (InputStream input = content.getResource().newInputStream())
{
IO.copy(input, out);
return;

View File

@ -39,6 +39,7 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.Resource;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
@ -91,6 +92,8 @@ public class HttpOutputTest
@AfterEach
public void destroy() throws Exception
{
IO.close(_handler._contentInputStream);
IO.close(_handler._contentChannel);
_server.stop();
_server.join();
}
@ -137,7 +140,7 @@ public class HttpOutputTest
public void testSendInputStreamSimple() throws Exception
{
Resource simple = Resource.newClassPathResource("simple/simple.txt");
_handler._contentInputStream = simple.getInputStream();
_handler._contentInputStream = simple.newInputStream();
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Content-Length: 11"));
@ -147,7 +150,7 @@ public class HttpOutputTest
public void testSendInputStreamBig() throws Exception
{
Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._contentInputStream = big.getInputStream();
_handler._contentInputStream = big.newInputStream();
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, Matchers.not(containsString("Content-Length")));
@ -158,7 +161,7 @@ public class HttpOutputTest
public void testSendInputStreamBigChunked() throws Exception
{
Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._contentInputStream = new FilterInputStream(big.getInputStream())
_handler._contentInputStream = new FilterInputStream(big.newInputStream())
{
@Override
public int read(byte[] b, int off, int len) throws IOException
@ -189,7 +192,7 @@ public class HttpOutputTest
public void testSendChannelSimple() throws Exception
{
Resource simple = Resource.newClassPathResource("simple/simple.txt");
_handler._contentChannel = simple.getReadableByteChannel();
_handler._contentChannel = simple.newReadableByteChannel();
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Content-Length: 11"));
@ -199,7 +202,7 @@ public class HttpOutputTest
public void testSendChannelBig() throws Exception
{
Resource big = Resource.newClassPathResource("simple/big.txt");
_handler._contentChannel = big.getReadableByteChannel();
_handler._contentChannel = big.newReadableByteChannel();
String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, Matchers.not(containsString("Content-Length")));
@ -232,7 +235,7 @@ public class HttpOutputTest
public void testSendChannelBigChunked() throws Exception
{
Resource big = Resource.newClassPathResource("simple/big.txt");
final ReadableByteChannel channel = big.getReadableByteChannel();
final ReadableByteChannel channel = big.newReadableByteChannel();
_handler._contentChannel = new ReadableByteChannel()
{
@Override

View File

@ -98,7 +98,7 @@ public class JDBCLoginService extends AbstractLoginService
{
Properties properties = new Properties();
Resource resource = Resource.newResource(_config);
try (InputStream in = resource.getInputStream())
try (InputStream in = resource.newInputStream())
{
properties.load(in);
}

View File

@ -225,7 +225,7 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
throw new IllegalStateException("Config does not exist: " + config);
Properties properties = new Properties();
try (InputStream inputStream = config.getInputStream())
try (InputStream inputStream = config.newInputStream())
{
properties.load(inputStream);
}

View File

@ -71,13 +71,13 @@ public class OrderingTest
}
@Override
public InputStream getInputStream() throws IOException
public InputStream newInputStream() throws IOException
{
return null;
}
@Override
public ReadableByteChannel getReadableByteChannel() throws IOException
public ReadableByteChannel newReadableByteChannel() throws IOException
{
return null;
}

View File

@ -83,14 +83,14 @@ public abstract class AbstractWebAppObjectInSessionTest extends AbstractSessionT
//File sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
File targetFile = new File(packageDirs, resourceName);
//copy(sourceFile, targetFile);
IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
IO.copy(resource.newInputStream(), new FileOutputStream(targetFile));
resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + "$" + WebAppObjectInSessionServlet.TestSharedStatic.class.getSimpleName() + ".class";
resource = Resource.newResource(getClass().getResource(resourceName));
//sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
targetFile = new File(packageDirs, resourceName);
//copy(sourceFile, targetFile);
IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
IO.copy(resource.newInputStream(), new FileOutputStream(targetFile));
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);