8000 fix: Do not lock the session while transferring content (#21437) · vaadin/flow@913f7cd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 913f7cd

Browse files
authored
fix: Do not lock the session while transferring content (#21437)
For common cases let's not lock the session for data transfer and assume that this locking can be done in a custom handler, if required.
1 parent e99b2cb commit 913f7cd

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

flow-server/src/main/java/com/vaadin/flow/server/streams/TransferProgressListener.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ static long transfer(InputStream inputStream, OutputStream outputStream,
185185
listeners.size());
186186
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
187187
int read;
188-
while ((read = read(transferContext.session(), inputStream,
189-
buffer)) >= 0) {
188+
while ((read = inputStream.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
190189
outputStream.write(buffer, 0, read);
191190
if (transferred < Long.MAX_VALUE) {
192191
try {
@@ -214,29 +213,4 @@ static long transfer(InputStream inputStream, OutputStream outputStream,
214213
finalTransferred));
215214
return transferred;
216215
}
217-
218-
/**
219-
* Read buffer amount of bytes from the input stream.
220-
*
221-
* @param session
222-
* vaadin session in use
223-
* @param source
224-
* input stream source
225-
* @param buffer
226-
* byte buffer to read into
227-
* @return amount of bytes read into buffer
228-
* @throws IOException
229-
* If the first byte cannot be read for any reason other than
230-
* the end of the file, if the input stream has been closed, or
231-
* if some other I/O error occurs.
232-
*/
233-
static int read(VaadinSession session, InputStream source, byte[] buffer)
234-
throws IOException {
235-
session.lock();
236-
try {
237-
return source.read(buffer, 0, DEFAULT_BUFFER_SIZE);
238-
} finally {
239-
session.unlock();
240-
}
241-
}
242216
}

flow-server/src/test/java/com/vaadin/flow/server/streams/AbstractDownloadHandlerTest.java

Lines changed: 16 additions & 0 deletions
CA83
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.ArrayList;
25+
import java.util.Collection;
2526
import java.util.List;
2627
import java.util.Optional;
2728
import java.util.concurrent.atomic.AtomicBoolean;
@@ -156,6 +157,21 @@ public void whenComplete() {
156157
Mockito.verify(completeHandler).accept(false);
157158
}
158159

160+
@Test
161+
public void transferProgressListener_transfer_sessionNotLocked()
162+
throws IOException {
163+
ByteArrayInputStream inputStream = new ByteArrayInputStream(
164+
"Hello".getBytes(StandardCharsets.UTF_8));
165+
VaadinSession session = Mockito.mock(VaadinSession.class);
166+
TransferContext context = Mockito.mock(TransferContext.class);
167+
Mockito.when(context.session()).thenReturn(session);
168+
OutputStream outputStream = Mockito.mock(OutputStream.class);
169+
Collection<TransferProgressListener> listeners = new ArrayList<>();
170+
TransferProgressListener.transfer(inputStream, outputStream, context,
171+
listeners);
172+
Mockito.verify(session, Mockito.times(0)).lock();
173+
}
174+
159175
@Test
160176
public void customHandlerWithShorthandCompleteListener_noErrorInTransfer_success_errorInTransfer_failure()
161177
throws IOException {

0 commit comments

Comments
 (0)
0