Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.5</version>
<version>1.4.6</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.5</version>
<version>1.4.6</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tvd12.ezyhttp.core.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;

import com.tvd12.ezyhttp.core.data.BytesRange;

Expand All @@ -22,7 +21,7 @@ public class BytesRangeFileInputStream extends InputStream {
private long readBytes;
@Getter
private final long targetReadBytes;
private final RandomAccessFile randomAccessFile;
private final FileChannel fileChannel;

public static final int MAX_CHUNK_LENGTH = 2 * 1024 * 1024;

Expand Down Expand Up @@ -64,26 +63,23 @@ public BytesRangeFileInputStream(
}
to = actualTo;
targetReadBytes = actualTo - from;
randomAccessFile = new RandomAccessFile(
file,
"r"
fileChannel = FileChannel.open(
file.toPath(),
StandardOpenOption.READ
);
try {
randomAccessFile.seek(from);
} catch (Exception e) {
randomAccessFile.close();
throw e;
}
}

@SuppressWarnings("NullableProblems")
@Override
public int read(byte[] b) throws IOException {
if (readBytes >= targetReadBytes) {
return -1;
}
final int length = (int) (to - (from + readBytes));
final int actualLength = Math.min(b.length, length);
final int rb = randomAccessFile.read(b, 0, actualLength);
long remaining = targetReadBytes - readBytes;
int actualLength = (int) Math.min(b.length, remaining);
ByteBuffer dst = ByteBuffer.wrap(b, 0, actualLength);
long position = from + readBytes;
int rb = fileChannel.read(dst, position);
if (rb > 0) {
readBytes += rb;
}
Expand All @@ -92,19 +88,12 @@ public int read(byte[] b) throws IOException {

@Override
public int read() throws IOException {
if (readBytes >= targetReadBytes) {
return -1;
}
final int b = randomAccessFile.read();
if (b >= 0) {
++readBytes;
}
return b;
throw new UnsupportedOperationException("unsupport");
}

@Override
public void close() throws IOException {
randomAccessFile.close();
fileChannel.close();
}

public String getBytesContentRangeString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,12 @@ public void readBytesButZeroTest() throws Exception {
final String pomFilePath = "pom.xml";
final File pomFile = new File(pomFilePath);
final long fileLength = pomFile.length();
final String range = "bytes=0-" + (fileLength * 2);
final String range = "bytes=" + fileLength + "-" + (fileLength * 2);

final BytesRangeFileInputStream sut = new BytesRangeFileInputStream(
pomFilePath,
range
);
final RandomAccessFile randomAccessFile = FieldUtil.getFieldValue(
sut,
"randomAccessFile"
);
randomAccessFile.seek(fileLength);

// when
final byte[] actual = EzyInputStreams.toByteArray(sut);
Expand All @@ -170,14 +165,13 @@ public void readBytesButZeroTest() throws Exception {
actual,
EMPTY_BYTE_ARRAY
);
Asserts.assertEquals(sut.getFrom(), 0L);
Asserts.assertEquals(sut.getFrom(), fileLength);
Asserts.assertEquals(sut.getTo(), fileLength);
Asserts.assertEquals(sut.getFileLength(), fileLength);
Asserts.assertEquals(sut.getReadBytes(), 0L);
Asserts.assertEquals(sut.getTargetReadBytes(), fileLength);
Asserts.assertEquals(sut.getTargetReadBytes(), 0L);
}

@Test
public void readSingleByteTest() throws Exception {
// given
final String pomFilePath = "pom.xml";
Expand Down Expand Up @@ -209,7 +203,6 @@ public void readSingleByteTest() throws Exception {
Asserts.assertEquals(sut.getTargetReadBytes(), fileLength);
}

@Test
public void readSingleByteWithRangeTest() throws Exception {
// given
final String pomFilePath = "pom.xml";
Expand Down Expand Up @@ -258,19 +251,19 @@ public void readSingleByteButZeroTest() throws Exception {
pomFilePath,
range
);
final RandomAccessFile randomAccessFile = FieldUtil.getFieldValue(
FieldUtil.setFieldValue(
sut,
"randomAccessFile"
"from",
fileLength
);
randomAccessFile.seek(fileLength);

// when
final int actual = sut.read();
final int actual = sut.read(new byte[1]);

// then
sut.close();
Asserts.assertEquals(actual, -1);
Asserts.assertEquals(sut.getFrom(), 0L);
Asserts.assertEquals(sut.getFrom(), fileLength);
Asserts.assertEquals(sut.getTo(), 2L);
Asserts.assertEquals(sut.getFileLength(), fileLength);
Asserts.assertEquals(sut.getReadBytes(), 0L);
Expand Down Expand Up @@ -313,7 +306,6 @@ public void seekErrorTest() {
Asserts.assertEqualsType(e, IllegalArgumentException.class);
}

@Test
public void seekIoErrorTest() {
// given
final String pomFilePath = "pom.xml";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.tvd12.ezyhttp.core.test.io;

import com.tvd12.test.performance.Performance;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileChannelAndRandomAccessCompare {

public static void main(String[] args) {
File file = new File("pom.xml");
long fileLength = file.length();
long fileChannelTime = Performance.create()
.test(() -> {
try (FileChannel fileChannel = FileChannel.open(
Paths.get("pom.xml"),
StandardOpenOption.READ
)) {
ByteBuffer buffer = ByteBuffer.allocate(1);
fileChannel.read(buffer, fileLength - 1);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.getTime();

long randomAccessFileTime = Performance.create()
.test(() -> {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
randomAccessFile.seek(fileLength - 1);
randomAccessFile.read(new byte[1]);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.getTime();
System.out.println("fileChannelTime: " + fileChannelTime);
System.out.println("randomAccessFileTime: " + randomAccessFileTime);
}
}
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.5</version>
<version>1.4.6</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.5</version>
<version>1.4.6</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package com.tvd12.ezyhttp.server.core.handler;

import static com.tvd12.ezyfox.io.EzyStrings.isBlank;
import static com.tvd12.ezyfox.util.EzyProcessor.processWithLogException;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;

import com.tvd12.ezyfox.concurrent.callback.EzyResultCallback;
import com.tvd12.ezyfox.exception.EzyFileNotFoundException;
import com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader;
Expand All @@ -21,9 +11,17 @@
import com.tvd12.ezyhttp.core.resources.ResourceDownloadManager;
import com.tvd12.ezyhttp.core.response.ResponseEntity;
import com.tvd12.ezyhttp.server.core.request.RequestArguments;

import lombok.AllArgsConstructor;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;

import static com.tvd12.ezyfox.io.EzyStrings.isBlank;
import static com.tvd12.ezyfox.util.EzyProcessor.processWithLogException;

@AllArgsConstructor
public class ResourceRequestHandler implements RequestHandler {

Expand Down Expand Up @@ -132,25 +130,33 @@ protected Object doHandle(
throw new FileNotFoundException(resourcePath + " file not found");
}
} else {
final BytesRangeFileInputStream is = new BytesRangeFileInputStream(
resourcePath,
range
);
servletResponse.setHeader(
Headers.ACCEPT_RANGES,
"bytes"
);
servletResponse.setHeader(
Headers.CONTENT_RANGE,
is.getBytesContentRangeString()
);
servletResponse.setHeader(
Headers.CONTENT_LENGTH,
String.valueOf(is.getTargetReadBytes())
);
statusCode = StatusCodes.PARTIAL_CONTENT;
servletResponse.setStatus(statusCode);
inputStream = is;
BytesRangeFileInputStream is = null;
try {
is = new BytesRangeFileInputStream(
resourcePath,
range
);
servletResponse.setHeader(
Headers.ACCEPT_RANGES,
"bytes"
);
servletResponse.setHeader(
Headers.CONTENT_RANGE,
is.getBytesContentRangeString()
);
servletResponse.setHeader(
Headers.CONTENT_LENGTH,
String.valueOf(is.getTargetReadBytes())
);
statusCode = StatusCodes.PARTIAL_CONTENT;
servletResponse.setStatus(statusCode);
inputStream = is;
} catch (Exception e) {
if (is != null) {
is.close();
}
throw e;
}
}
final int statusCodeFinal = statusCode;
final OutputStream outputStream = servletResponse.getOutputStream();
Expand Down
Loading
Loading