Skip to content

Commit 21cfe6d

Browse files
authored
Merge pull request #2169 from kofemann/file-region-for-master
introduce FileChunk interface to transfer file chunks
2 parents 0c82a6a + 0117608 commit 21cfe6d

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2018 Payara Services Ltd.
4+
*
5+
* This program and the accompanying materials are made available under the
6+
* terms of the Eclipse Public License v. 2.0, which is available at
7+
* http://www.eclipse.org/legal/epl-2.0.
8+
*
9+
* This Source Code may also be made available under the following Secondary
10+
* Licenses when the conditions for such availability set forth in the
11+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12+
* version 2 with the GNU Classpath Exception, which is available at
13+
* https://www.gnu.org/software/classpath/license.html.
14+
*
15+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16+
*/
17+
18+
package org.glassfish.grizzly;
19+
20+
import java.io.IOException;
21+
import java.nio.channels.WritableByteChannel;
22+
import org.glassfish.grizzly.asyncqueue.WritableMessage;
23+
24+
public interface FileChunk extends WritableMessage {
25+
26+
/**
27+
* Transfers the File region backing this <code>FileRegion</code> to the specified {@link WritableByteChannel}.
28+
*
29+
* @param c the {@link WritableByteChannel}
30+
* @return the number of bytes that have been transferred
31+
* @throws IOException if an error occurs while processing
32+
* @see java.nio.channels.FileChannel#transferTo(long, long, java.nio.channels.WritableByteChannel)
33+
*/
34+
long writeTo(final WritableByteChannel c) throws IOException;
35+
36+
}

modules/grizzly/src/main/java/org/glassfish/grizzly/FileTransfer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2018 Payara Services Ltd.
44
*
55
* This program and the accompanying materials are made available under the
@@ -24,15 +24,13 @@
2424
import java.nio.channels.FileChannel;
2525
import java.nio.channels.WritableByteChannel;
2626

27-
import org.glassfish.grizzly.asyncqueue.WritableMessage;
28-
2927
/**
3028
* A simple class that abstracts {@link FileChannel#transferTo(long, long, java.nio.channels.WritableByteChannel)} for
3129
* use with Grizzly 2.0 {@link org.glassfish.grizzly.asyncqueue.AsyncQueueWriter}.
3230
*
3331
* @since 2.2
3432
*/
35-
public class FileTransfer implements WritableMessage {
33+
public class FileTransfer implements FileChunk {
3634

3735
private FileChannel fileChannel;
3836
private long len;

modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOAsyncQueueWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.glassfish.grizzly.CloseReason;
3131
import org.glassfish.grizzly.CloseType;
3232
import org.glassfish.grizzly.Connection;
33-
import org.glassfish.grizzly.FileTransfer;
33+
import org.glassfish.grizzly.FileChunk;
3434
import org.glassfish.grizzly.Grizzly;
3535
import org.glassfish.grizzly.IOEvent;
3636
import org.glassfish.grizzly.WriteResult;
@@ -102,8 +102,8 @@ protected long write0(final NIOConnection connection, final WritableMessage mess
102102
((TCPNIOConnection) connection).terminate0(null, new CloseReason(CloseType.REMOTELY, e));
103103
throw e;
104104
}
105-
} else if (message instanceof FileTransfer) {
106-
written = ((FileTransfer) message).writeTo((SocketChannel) connection.getChannel());
105+
} else if (message instanceof FileChunk) {
106+
written = ((FileChunk) message).writeTo((SocketChannel) connection.getChannel());
107107
((TCPNIOConnection) connection).onWrite(null, written);
108108
} else {
109109
throw new IllegalStateException("Unhandled message type");

modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/TCPNIOTransport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.glassfish.grizzly.Connection;
4242
import org.glassfish.grizzly.Context;
4343
import org.glassfish.grizzly.EmptyCompletionHandler;
44-
import org.glassfish.grizzly.FileTransfer;
44+
import org.glassfish.grizzly.FileChunk;
4545
import org.glassfish.grizzly.Grizzly;
4646
import org.glassfish.grizzly.GrizzlyFuture;
4747
import org.glassfish.grizzly.IOEvent;
@@ -635,8 +635,8 @@ public int write(final TCPNIOConnection connection, final WritableMessage messag
635635
connection.terminate0(null, new CloseReason(CloseType.REMOTELY, e));
636636
throw e;
637637
}
638-
} else if (message instanceof FileTransfer) {
639-
written = (int) ((FileTransfer) message).writeTo((SocketChannel) connection.getChannel());
638+
} else if (message instanceof FileChunk) {
639+
written = (int) ((FileChunk) message).writeTo((SocketChannel) connection.getChannel());
640640
} else {
641641
throw new IllegalStateException("Unhandled message type");
642642
}

modules/grizzly/src/main/java/org/glassfish/grizzly/nio/transport/UDPNIOTransport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.glassfish.grizzly.Connection;
3939
import org.glassfish.grizzly.Context;
4040
import org.glassfish.grizzly.EmptyCompletionHandler;
41-
import org.glassfish.grizzly.FileTransfer;
41+
import org.glassfish.grizzly.FileChunk;
4242
import org.glassfish.grizzly.GracefulShutdownListener;
4343
import org.glassfish.grizzly.Grizzly;
4444
import org.glassfish.grizzly.GrizzlyFuture;
@@ -571,8 +571,8 @@ public long write(final UDPNIOConnection connection, final SocketAddress dstAddr
571571
}
572572

573573
connection.onWrite(buffer, (int) written);
574-
} else if (message instanceof FileTransfer) {
575-
written = ((FileTransfer) message).writeTo((DatagramChannel) connection.getChannel());
574+
} else if (message instanceof FileChunk) {
575+
written = ((FileChunk) message).writeTo((DatagramChannel) connection.getChannel());
576576
} else {
577577
throw new IllegalStateException("Unhandled message type");
578578
}

0 commit comments

Comments
 (0)