From 500b2b2ee461890d5cf4c9e45e9703712193812c Mon Sep 17 00:00:00 2001 From: jansupol Date: Wed, 20 Mar 2024 20:40:45 +0100 Subject: [PATCH] Fixed FirstByteCachingStream in JNH Connector Signed-off-by: jansupol --- .../jnh/connector/JavaNetHttpConnector.java | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java index 69d25eb66f..57296c6df9 100644 --- a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java +++ b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -360,24 +360,30 @@ private FirstByteCachingStream(InputStream inner) { @Override public int read() throws IOException { lock.lock(); - final int r = zero != -1 ? zero : inner.read(); - zero = -1; - lock.unlock(); - return r; + try { + final int r = zero != -1 ? zero : inner.read(); + zero = -1; + return r; + } finally { + lock.unlock(); + } } @Override public int read(byte[] b, int off, int len) throws IOException { lock.lock(); int r; - if (zero != -1) { - b[off] = (byte) (zero & 0xFF); - r = inner.read(b, off + 1, len - 1); - } else { - r = inner.read(b, off, len); + try { + if (zero != -1) { + b[off] = (byte) (zero & 0xFF); + r = inner.read(b, off + 1, len - 1); + } else { + r = inner.read(b, off, len); + } + zero = -1; + } finally { + lock.unlock(); } - zero = -1; - lock.unlock(); return r; } @@ -385,23 +391,24 @@ public int read(byte[] b, int off, int len) throws IOException { @Override public int available() throws IOException { lock.lock(); - if (zero != -1) { - lock.unlock(); - return 1; - } + try { + if (zero != -1) { + return 1; + } - int available = inner.available(); - if (available != 1) { - lock.unlock(); - return available; - } + int available = inner.available(); + if (available != 1) { + return available; + } - zero = inner.read(); - if (zero == -1) { - available = 0; + zero = inner.read(); + if (zero == -1) { + available = 0; + } + return available; + } finally { + lock.unlock(); } - lock.unlock(); - return available; } @Override @@ -418,10 +425,14 @@ public boolean markSupported() { } @Override - public synchronized void mark(int readlimit) { + public void mark(int readlimit) { inner.mark(readlimit); } + @Override + public void reset() throws IOException { + inner.reset(); + } } }