Skip to content

Commit c7992e4

Browse files
committed
Server supports large file transfer
1 parent c1b9d11 commit c7992e4

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

src/main/java/edu/nju/http/message/packer/MessagePacker.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ public class MessagePacker {
3333
HttpMessage message;
3434
private final
3535
String[] transferEncodings;
36+
private final
37+
String acceptEncoding;
38+
39+
private final
40+
ByteBuffer bb;
3641

3742
private
3843
AsynchronousSocketChannel socket;
3944
private
4045
Queue<byte[]> byteArrayQueue;
41-
private
42-
String acceptEncoding;
4346

4447
// ==================== Constructors ==================== //
4548

@@ -62,9 +65,9 @@ public MessagePacker(HttpMessage message, String[] transferEncodings, String acc
6265
this.transferEncodings = transferEncodings;
6366
this.socket = null;
6467
this.acceptEncoding = Objects.requireNonNullElseGet(acceptEncoding, ()->"");
68+
this.bb = ByteBuffer.allocate(Config.SOCKET_BUFFER_SIZE);
6569

6670
strategyMap = new HashMap<>();
67-
strategyMap.put( CONTENT_LENGTH, new TransContentLengthEncodeStrategy() );
6871
strategyMap.put( CHUNKED, new TransChunkedEncodeStrategy() );
6972
}
7073

@@ -140,40 +143,42 @@ private int send()
140143
}
141144

142145
// -------------------- 3. Send out start line and headers -------------------- //
143-
written += flush(message.getStartLineAndHeadersAsStream());
146+
written += flush(
147+
new SourceEncodeStrategy(message.getStartLineAndHeadersAsStream())
148+
);
144149

145150
// -------------------- 4. Send out -------------------- //
146-
for (byte[] bytes; (bytes = upperStrategy.readBytes()).length != 0; ) {
147-
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
148-
written += flush(stream);
149-
}
151+
written += flush(upperStrategy);
150152

151153
if (socket != null)
152154
Log.logSocket(socket,"Message sent %f KB ".formatted((double) written / (1 << 10)));
153155

154156
return written;
155157
}
156158

157-
private int flush(InputStream stream)
159+
private int flush(EncodeStrategy strategy)
158160
throws ExecutionException, InterruptedException, IOException {
159161
int written = 0;
160162

161163
if (socket != null) {
162-
for (byte[] bytes; (bytes = stream.readNBytes(Config.SOCKET_BUFFER_SIZE)).length != 0; ) {
164+
for (byte[] bytes ; (bytes = strategy.readBytes()).length != 0; ) {
165+
bb.clear();
166+
bb.put(bytes);
167+
bb.flip();
163168
try {
164-
var future = socket.write(
165-
ByteBuffer.wrap(bytes)
166-
);
167-
int delta = future.get();
168-
assert Log.testExpect("Bytes sent", bytes.length, delta);
169-
written += delta;
169+
for (int cur = 0, delta; cur < bb.limit(); cur += delta) {
170+
bb.position(cur);
171+
var future = socket.write(bb);
172+
delta = future.get();
173+
}
174+
written += bb.limit();
170175
} catch (WritePendingException e) {
171176
e.printStackTrace();
172177
}
173178
}
174179
} else {
175180
assert byteArrayQueue != null;
176-
byte[] bytes = stream.readAllBytes();
181+
byte[] bytes = strategy.readAllBytes();
177182
byteArrayQueue.offer(bytes);
178183
written = bytes.length;
179184
}

src/main/java/edu/nju/http/message/packer/encode/ContentGzipEncodeStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private void prepare() throws IOException {
1818
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(
1919
outputStream
2020
)) {
21-
byte[] bytes = upper.readBytes();
21+
byte[] bytes = upper.readAllBytes();
2222
gzipOutputStream.write(bytes, 0, bytes.length);
2323
gzipOutputStream.finish();
2424
}

src/main/java/edu/nju/http/message/packer/encode/SourceEncodeStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.nju.http.message.packer.encode;
22

3+
import edu.nju.http.util.Config;
4+
35
import java.io.ByteArrayInputStream;
46
import java.io.IOException;
57
import java.io.InputStream;
@@ -9,7 +11,7 @@ public class SourceEncodeStrategy extends EncodeStrategy {
911
InputStream inputStream;
1012

1113
private SourceEncodeStrategy(){
12-
inputStream = new ByteArrayInputStream(new byte[0]);
14+
this.inputStream = new ByteArrayInputStream(new byte[0]);
1315
}
1416

1517
public SourceEncodeStrategy(InputStream inputStream) {
@@ -23,7 +25,7 @@ public byte[] readAllBytes() throws IOException {
2325

2426
@Override
2527
public byte[] readBytes() throws IOException {
26-
return inputStream.readAllBytes();
28+
return inputStream.readNBytes(Config.SOCKET_BUFFER_SIZE);
2729
}
2830

2931
@Override

src/main/java/edu/nju/http/message/packer/encode/TransContentLengthEncodeStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import static edu.nju.http.message.consts.Headers.CONTENT_LENGTH;
1010

11+
@Deprecated
1112
public class TransContentLengthEncodeStrategy extends EncodeStrategy {
1213
private ByteArrayInputStream bis;
1314

src/main/java/edu/nju/http/util/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Config {
4545

4646
public final static int DEFAULT_TIMEOUT = 10000;
4747
public final static boolean DEFAULT_KEEP_ALIVE = false;
48-
public final static int SOCKET_BUFFER_SIZE = 1 << 30;
48+
public final static int SOCKET_BUFFER_SIZE = 1 << 20;
4949

5050
private static Path getPath(String resource) {
5151
URL url = ClassLoader.getSystemClassLoader().getResource(resource);

0 commit comments

Comments
 (0)