Jetty 12 - use JVM provided null OutputStream (#8789)

* Remove IO.getNull* methods and use JVM versions

+ This also honors the open/close/error on
  bad use of the streams/writers
This commit is contained in:
Joakim Erdfelt 2022-11-09 07:06:43 -06:00 committed by GitHub
parent e2e4d256e7
commit 08c47f5f57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 122 deletions

View File

@ -256,7 +256,9 @@ public class InputStreamResponseListener extends Listener.Adapter
InputStream result = new Input();
if (stream.compareAndSet(null, result))
return result;
return IO.getClosedStream();
result = InputStream.nullInputStream();
IO.close(result);
return result;
}
private List<Callback> drain()

View File

@ -16,6 +16,7 @@ package org.eclipse.jetty.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@ -277,7 +278,7 @@ public class MultiPartCaptureTest
MessageDigest digest = MessageDigest.getInstance("SHA1");
assertTrue(part.getContent().rewind());
try (InputStream partInputStream = Content.Source.asInputStream(part.getContent());
DigestOutputStream digester = new DigestOutputStream(IO.getNullStream(), digest))
DigestOutputStream digester = new DigestOutputStream(OutputStream.nullOutputStream(), digest))
{
IO.copy(partInputStream, digester);
String actualSha1sum = Hex.asHex(digest.digest()).toLowerCase(Locale.US);

View File

@ -559,120 +559,6 @@ public class IO
return total;
}
/**
* @return An outputstream to nowhere
*/
public static OutputStream getNullStream()
{
return __nullStream;
}
/**
* @return An outputstream to nowhere
*/
public static InputStream getClosedStream()
{
return __closedStream;
}
private static class NullOS extends OutputStream
{
@Override
public void close()
{
}
@Override
public void flush()
{
}
@Override
public void write(byte[] b)
{
}
@Override
public void write(byte[] b, int i, int l)
{
}
@Override
public void write(int b)
{
}
}
private static NullOS __nullStream = new NullOS();
private static class ClosedIS extends InputStream
{
@Override
public int read() throws IOException
{
return -1;
}
}
private static ClosedIS __closedStream = new ClosedIS();
/**
* @return An writer to nowhere
*/
public static Writer getNullWriter()
{
return __nullWriter;
}
/**
* @return An writer to nowhere
*/
public static PrintWriter getNullPrintWriter()
{
return __nullPrintWriter;
}
private static class NullWrite extends Writer
{
@Override
public void close()
{
}
@Override
public void flush()
{
}
@Override
public void write(char[] b)
{
}
@Override
public void write(char[] b, int o, int l)
{
}
@Override
public void write(int b)
{
}
@Override
public void write(String s)
{
}
@Override
public void write(String s, int o, int l)
{
}
}
private static NullWrite __nullWriter = new NullWrite();
private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);
}

View File

@ -963,7 +963,7 @@ public class AsyncMiddleManServletTest
{
// Consume the stream once.
InputStream input = source.getInputStream();
IO.copy(input, IO.getNullStream());
IO.copy(input, OutputStream.nullOutputStream());
// Reset the stream and re-read it.
input.reset();
@ -1116,7 +1116,7 @@ public class AsyncMiddleManServletTest
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{
IO.copy(request.getInputStream(), IO.getNullStream());
IO.copy(request.getInputStream(), OutputStream.nullOutputStream());
}
});
CountDownLatch destroyLatch = new CountDownLatch(1);

View File

@ -963,7 +963,7 @@ public class AsyncMiddleManServletTest
{
// Consume the stream once.
InputStream input = source.getInputStream();
IO.copy(input, IO.getNullStream());
IO.copy(input, OutputStream.nullOutputStream());
// Reset the stream and re-read it.
input.reset();
@ -1116,7 +1116,7 @@ public class AsyncMiddleManServletTest
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{
IO.copy(request.getInputStream(), IO.getNullStream());
IO.copy(request.getInputStream(), OutputStream.nullOutputStream());
}
});
CountDownLatch destroyLatch = new CountDownLatch(1);

View File

@ -15,6 +15,7 @@ package org.eclipse.jetty.ee9.security.authentication;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
@ -33,7 +34,6 @@ import org.eclipse.jetty.ee9.security.LoginService;
import org.eclipse.jetty.ee9.security.SecurityHandler;
import org.eclipse.jetty.ee9.security.ServerAuthException;
import org.eclipse.jetty.ee9.security.UserAuthentication;
import org.eclipse.jetty.util.IO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -274,7 +274,7 @@ public class DeferredAuthentication implements Authentication.Deferred
@Override
public PrintWriter getWriter() throws IOException
{
return IO.getNullPrintWriter();
return new PrintWriter(Writer.nullWriter());
}
@Override