From 5ee1016989a7cf7cd1ad0d403f56f491096bb87f Mon Sep 17 00:00:00 2001 From: "robin.bygrave" Date: Tue, 3 Dec 2024 15:36:56 +1300 Subject: [PATCH 1/3] final class for ActivityTimer, ChunkedInputStream, ChunkedOutputStream --- src/main/java/robaho/net/httpserver/ActivityTimer.java | 6 +++--- .../java/robaho/net/httpserver/ChunkedInputStream.java | 7 ++++++- .../java/robaho/net/httpserver/ChunkedOutputStream.java | 6 +++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/robaho/net/httpserver/ActivityTimer.java b/src/main/java/robaho/net/httpserver/ActivityTimer.java index 3d62e02..ca9f1e7 100644 --- a/src/main/java/robaho/net/httpserver/ActivityTimer.java +++ b/src/main/java/robaho/net/httpserver/ActivityTimer.java @@ -6,7 +6,7 @@ import java.util.Locale; import java.util.TimerTask; -class ActivityTimer { +final class ActivityTimer { private static volatile long now = System.currentTimeMillis(); private static volatile String dateAndTime = formatDate(); @@ -39,14 +39,14 @@ private static String formatDate() { return sb.toString(); } - public static long now() { + static long now() { return now; } /** * return the formatted current date and time suitable for use with the Date http header. this * is OK to cache since the resolution is only seconds, and we will update more often than that */ - public static String dateAndTime() { + static String dateAndTime() { return dateAndTime; } diff --git a/src/main/java/robaho/net/httpserver/ChunkedInputStream.java b/src/main/java/robaho/net/httpserver/ChunkedInputStream.java index e9fce46..6e0bac0 100644 --- a/src/main/java/robaho/net/httpserver/ChunkedInputStream.java +++ b/src/main/java/robaho/net/httpserver/ChunkedInputStream.java @@ -27,7 +27,7 @@ import java.io.*; -class ChunkedInputStream extends LeftOverInputStream { +final class ChunkedInputStream extends LeftOverInputStream { ChunkedInputStream(ExchangeImpl t, InputStream src) { super(t, src); } @@ -107,6 +107,7 @@ private int readChunkHeader() throws IOException { throw new IOException("end of stream reading chunk header"); } + @Override protected int readImpl(byte[] b, int off, int len) throws IOException { if (eof) { return -1; @@ -154,6 +155,7 @@ private void consumeCRLF() throws IOException { * limitation for the moment. It only affects potential efficiency * rather than correctness. */ + @Override public int available() throws IOException { if (eof || closed) { return 0; @@ -162,13 +164,16 @@ public int available() throws IOException { return n > remaining ? remaining : n; } + @Override public boolean markSupported() { return false; } + @Override public void mark(int l) { } + @Override public void reset() throws IOException { throw new IOException("mark/reset not supported"); } diff --git a/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java b/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java index 8f0f753..6d6cc21 100644 --- a/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java +++ b/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java @@ -45,7 +45,7 @@ * 0\r\n\r\n */ -class ChunkedOutputStream extends FilterOutputStream { +final class ChunkedOutputStream extends FilterOutputStream { private boolean closed = false; /* max. amount of user data per chunk */ static final int CHUNK_SIZE = 4096; @@ -61,6 +61,7 @@ class ChunkedOutputStream extends FilterOutputStream { this.t = t; } + @Override public void write(int b) throws IOException { if (closed) { throw new StreamClosedException(); @@ -73,6 +74,7 @@ public void write(int b) throws IOException { assert count < CHUNK_SIZE; } + @Override public void write(byte[] b, int off, int len) throws IOException { Objects.checkFromIndexSize(off, len, b.length); if (len == 0) { @@ -128,6 +130,7 @@ private void writeChunk() throws IOException { pos = OFFSET; } + @Override public void close() throws IOException { if (closed) { return; @@ -149,6 +152,7 @@ public void close() throws IOException { } } + @Override public void flush() throws IOException { if (closed) { throw new StreamClosedException(); From 2c3add11f2502c5b11f8b0cf4c6299defa9efcfb Mon Sep 17 00:00:00 2001 From: "robin.bygrave" Date: Tue, 10 Dec 2024 12:54:01 +1300 Subject: [PATCH 2/3] final class for ActivityTimer Revert changes for ChunkedInputStream, ChunkedOutputStream --- .../java/robaho/net/httpserver/ChunkedInputStream.java | 7 +------ .../java/robaho/net/httpserver/ChunkedOutputStream.java | 6 +----- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/main/java/robaho/net/httpserver/ChunkedInputStream.java b/src/main/java/robaho/net/httpserver/ChunkedInputStream.java index 6e0bac0..e9fce46 100644 --- a/src/main/java/robaho/net/httpserver/ChunkedInputStream.java +++ b/src/main/java/robaho/net/httpserver/ChunkedInputStream.java @@ -27,7 +27,7 @@ import java.io.*; -final class ChunkedInputStream extends LeftOverInputStream { +class ChunkedInputStream extends LeftOverInputStream { ChunkedInputStream(ExchangeImpl t, InputStream src) { super(t, src); } @@ -107,7 +107,6 @@ private int readChunkHeader() throws IOException { throw new IOException("end of stream reading chunk header"); } - @Override protected int readImpl(byte[] b, int off, int len) throws IOException { if (eof) { return -1; @@ -155,7 +154,6 @@ private void consumeCRLF() throws IOException { * limitation for the moment. It only affects potential efficiency * rather than correctness. */ - @Override public int available() throws IOException { if (eof || closed) { return 0; @@ -164,16 +162,13 @@ public int available() throws IOException { return n > remaining ? remaining : n; } - @Override public boolean markSupported() { return false; } - @Override public void mark(int l) { } - @Override public void reset() throws IOException { throw new IOException("mark/reset not supported"); } diff --git a/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java b/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java index 6d6cc21..8f0f753 100644 --- a/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java +++ b/src/main/java/robaho/net/httpserver/ChunkedOutputStream.java @@ -45,7 +45,7 @@ * 0\r\n\r\n */ -final class ChunkedOutputStream extends FilterOutputStream { +class ChunkedOutputStream extends FilterOutputStream { private boolean closed = false; /* max. amount of user data per chunk */ static final int CHUNK_SIZE = 4096; @@ -61,7 +61,6 @@ final class ChunkedOutputStream extends FilterOutputStream { this.t = t; } - @Override public void write(int b) throws IOException { if (closed) { throw new StreamClosedException(); @@ -74,7 +73,6 @@ public void write(int b) throws IOException { assert count < CHUNK_SIZE; } - @Override public void write(byte[] b, int off, int len) throws IOException { Objects.checkFromIndexSize(off, len, b.length); if (len == 0) { @@ -130,7 +128,6 @@ private void writeChunk() throws IOException { pos = OFFSET; } - @Override public void close() throws IOException { if (closed) { return; @@ -152,7 +149,6 @@ public void close() throws IOException { } } - @Override public void flush() throws IOException { if (closed) { throw new StreamClosedException(); From d20dc990d6a2f1b6c3e9ef7b72403030182b4c6a Mon Sep 17 00:00:00 2001 From: "robin.bygrave" Date: Tue, 10 Dec 2024 13:09:59 +1300 Subject: [PATCH 3/3] Add @Override to NoSyncBufferedInputStream --- .../robaho/net/httpserver/NoSyncBufferedInputStream.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/robaho/net/httpserver/NoSyncBufferedInputStream.java b/src/main/java/robaho/net/httpserver/NoSyncBufferedInputStream.java index 888c1bc..d6b3cd9 100644 --- a/src/main/java/robaho/net/httpserver/NoSyncBufferedInputStream.java +++ b/src/main/java/robaho/net/httpserver/NoSyncBufferedInputStream.java @@ -100,6 +100,7 @@ private void fill() throws IOException { * or an I/O error occurs. * @see java.io.FilterInputStream#in */ + @Override public int read() throws IOException { if (pos >= count) { fill(); @@ -167,6 +168,7 @@ private int read1(byte[] b, int off, int len) throws IOException { * or an I/O error occurs. * @throws IndexOutOfBoundsException {@inheritDoc} */ + @Override public int read(byte[] b, int off, int len) throws IOException { ensureOpen(); if ((off | len | (off + len) | (b.length - (off + len))) < 0) { @@ -199,6 +201,7 @@ public int read(byte[] b, int off, int len) throws IOException { * {@code in.skip(n)} throws an IOException, * or an I/O error occurs. */ + @Override public long skip(long n) throws IOException { ensureOpen(); if (n <= 0) { @@ -229,6 +232,7 @@ public long skip(long n) throws IOException { * invoking its {@link #close()} method, * or an I/O error occurs. */ + @Override public int available() throws IOException { if (in == null) throw new IOException("Stream closed"); @@ -248,6 +252,7 @@ public int available() throws IOException { * * @throws IOException if an I/O error occurs. */ + @Override public void close() throws IOException { if(buf!=null) { buf = null;