Skip to content

Commit

Permalink
Jetty 12 - use JVM provided null OutputStream (#8789)
Browse files Browse the repository at this point in the history
* Remove IO.getNull* methods and use JVM versions

+ This also honors the open/close/error on
  bad use of the streams/writers
  • Loading branch information
joakime authored Nov 9, 2022
1 parent e2e4d25 commit 08c47f5
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ public InputStream getInputStream()
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -277,7 +278,7 @@ private void assertParts(Map<String, List<MultiPart.Part>> allParts) throws Exce
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);
Expand Down
114 changes: 0 additions & 114 deletions jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,120 +559,6 @@ public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int off

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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public boolean transform(Source source, Sink sink) throws IOException
{
// 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();
Expand Down Expand Up @@ -1116,7 +1116,7 @@ public void testAfterContentTransformerClosingFilesOnClientRequestException() th
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public boolean transform(Source source, Sink sink) throws IOException
{
// 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();
Expand Down Expand Up @@ -1116,7 +1116,7 @@ public void testAfterContentTransformerClosingFilesOnClientRequestException() th
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
Expand All @@ -33,7 +34,6 @@
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;

Expand Down Expand Up @@ -274,7 +274,7 @@ public ServletOutputStream getOutputStream() throws IOException
@Override
public PrintWriter getWriter() throws IOException
{
return IO.getNullPrintWriter();
return new PrintWriter(Writer.nullWriter());
}

@Override
Expand Down

0 comments on commit 08c47f5

Please sign in to comment.