Get file size efficiently in Java (#14970)
* Get file size efficiently in Java * Get file size efficiently in Java * Get file size efficiently in Java * Update FileSizeBenchmark.java
This commit is contained in:
parent
ec26c2802c
commit
3178d8cb59
@ -0,0 +1,67 @@
|
||||
package com.baeldung.sizebenchmark;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.SingleShotTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.NANOSECONDS)
|
||||
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.NANOSECONDS)
|
||||
public class FileSizeBenchmark {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void getFileSizeUsingLengthMethod(Blackhole blackhole) throws Exception {
|
||||
File file = new File("src/test/resources/size/sample_file_1.in");
|
||||
blackhole.consume(file.length());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void getFileSizeUsingFileInputStream(Blackhole blackhole) throws Exception {
|
||||
try (FileInputStream fis = new FileInputStream("src/test/resources/size/sample_file_1.in")) {
|
||||
long result = fis.getChannel().size();
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void getFileSizeUsingInputStreamAndUrl(Blackhole blackhole) throws Exception {
|
||||
File me = new File("src/test/resources/size/sample_file_1.in");
|
||||
URL url = me.toURI().toURL();
|
||||
|
||||
try (InputStream stream = url.openStream()) {
|
||||
blackhole.consume(stream.available());
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void getFileSizeUsingApacheCommon(Blackhole blackhole) {
|
||||
File imageFile = new File("src/test/resources/size/sample_file_1.in");
|
||||
long size = FileUtils.sizeOf(imageFile);
|
||||
blackhole.consume(size);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void getFileSizeUsingFileChannel(Blackhole blackhole) throws IOException {
|
||||
Path imageFilePath = Paths.get("src/test/resources/size/sample_file_1.in");
|
||||
try (FileChannel imageFileChannel = FileChannel.open(imageFilePath)) {
|
||||
long imageFileSize = imageFileChannel.size();
|
||||
blackhole.consume(imageFileSize);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,10 @@ package com.baeldung.size;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -62,4 +65,24 @@ public class JavaFileSizeUnitTest {
|
||||
final long length = file.length();
|
||||
return length;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeUsingFileInputStream_thenCorrect() throws IOException {
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(filePath)) {
|
||||
long result = fis.getChannel().size();
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, result);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeUsingUrlAndInputStream_thenCorrect() throws IOException {
|
||||
|
||||
File file = new File(filePath);
|
||||
URL url = file.toURI().toURL();
|
||||
|
||||
try (InputStream stream = url.openStream()) {
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, stream.available());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user