summaryrefslogtreecommitdiffstats
path: root/vespajlib/src
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-08-30 12:11:35 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2021-08-30 12:11:35 +0200
commit37eaa36494321b3d3c1427fabf34ebe020f4b9ac (patch)
tree6868462525801a65eab185fa1577d891772e8546 /vespajlib/src
parent11e7a365a7f6e63b109f24f3f81ff4aaee9bad72 (diff)
lz4 compress blobs.
Diffstat (limited to 'vespajlib/src')
-rw-r--r--vespajlib/src/main/java/com/yahoo/compress/Compressor.java25
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/IOUtils.java31
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java6
3 files changed, 38 insertions, 24 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/compress/Compressor.java b/vespajlib/src/main/java/com/yahoo/compress/Compressor.java
index ad51ac1ca38..98228292cc7 100644
--- a/vespajlib/src/main/java/com/yahoo/compress/Compressor.java
+++ b/vespajlib/src/main/java/com/yahoo/compress/Compressor.java
@@ -82,22 +82,25 @@ public class Compressor {
* @throws IllegalArgumentException if the compression type is not supported
*/
public Compression compress(CompressionType requestedCompression, byte[] data, Optional<Integer> uncompressedSize) {
+ return compress(requestedCompression, data, 0, uncompressedSize.orElse(data.length));
+ }
+ public Compression compress(CompressionType requestedCompression, byte[] data, int offset, int len) {
switch (requestedCompression) {
case NONE:
- data = uncompressedSize.isPresent() ? Arrays.copyOf(data, uncompressedSize.get()) : data;
+ if ((offset != 0) || (len != data.length)) {
+ data = Arrays.copyOfRange(data, offset, offset + len);
+ }
return new Compression(CompressionType.NONE, data.length, data);
case LZ4:
- int dataSize = uncompressedSize.isPresent() ? uncompressedSize.get() : data.length;
- if (dataSize < compressMinSizeBytes) return new Compression(CompressionType.INCOMPRESSIBLE, dataSize, data);
- byte[] compressedData = getCompressor().compress(data, 0, dataSize);
- if (compressedData.length + 8 >= dataSize * compressionThresholdFactor)
- return new Compression(CompressionType.INCOMPRESSIBLE, dataSize, data);
- return new Compression(CompressionType.LZ4, dataSize, compressedData);
+ if (len < compressMinSizeBytes) return new Compression(CompressionType.INCOMPRESSIBLE, len, data);
+ byte[] compressedData = getCompressor().compress(data, offset, len);
+ if (compressedData.length + 8 >= len * compressionThresholdFactor)
+ return new Compression(CompressionType.INCOMPRESSIBLE, len, data);
+ return new Compression(CompressionType.LZ4, len, compressedData);
case ZSTD:
- int dataLength = uncompressedSize.orElse(data.length);
- if (dataLength < compressMinSizeBytes) return new Compression(CompressionType.INCOMPRESSIBLE, dataLength, data);
- byte[] compressed = zstdCompressor.compress(data, 0, dataLength);
- return new Compression(CompressionType.ZSTD, dataLength, compressed);
+ if (len < compressMinSizeBytes) return new Compression(CompressionType.INCOMPRESSIBLE, len, data);
+ byte[] compressed = zstdCompressor.compress(data, offset, len);
+ return new Compression(CompressionType.ZSTD, len, compressed);
default:
throw new IllegalArgumentException(requestedCompression + " is not supported");
}
diff --git a/vespajlib/src/main/java/com/yahoo/io/IOUtils.java b/vespajlib/src/main/java/com/yahoo/io/IOUtils.java
index fa32ec1ff9c..64140ab9706 100644
--- a/vespajlib/src/main/java/com/yahoo/io/IOUtils.java
+++ b/vespajlib/src/main/java/com/yahoo/io/IOUtils.java
@@ -2,8 +2,25 @@
package com.yahoo.io;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.UncheckedIOException;
+import java.io.Writer;
import java.nio.channels.FileChannel;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.util.List;
@@ -19,7 +36,7 @@ import java.nio.ByteBuffer;
*/
public abstract class IOUtils {
- static private final Charset utf8Charset = Charset.forName("utf-8");
+ static private final Charset utf8Charset = StandardCharsets.UTF_8;
/** Closes a writer, or does nothing if the writer is null */
public static void closeWriter(Writer writer) {
@@ -341,7 +358,7 @@ public abstract class IOUtils {
public static String readFile(File file) throws IOException {
try {
if (file == null) return null;
- return new String(Files.readAllBytes(file.toPath()), "utf-8");
+ return Files.readString(file.toPath(), utf8Charset);
}
catch (NoSuchFileException e) {
throw new NoSuchFileException("Could not find file '" + file.getAbsolutePath() + "'");
@@ -370,9 +387,7 @@ public abstract class IOUtils {
if (lengthL>Integer.MAX_VALUE)
throw new IllegalArgumentException("File too big for byte array: "+file.getCanonicalPath());
- InputStream in = null;
- try {
- in = new FileInputStream(file);
+ try (InputStream in = new FileInputStream(file)) {
int length = (int)lengthL;
byte[] array = new byte[length];
int offset = 0;
@@ -381,10 +396,6 @@ public abstract class IOUtils {
offset += count;
return array;
}
- finally {
- if (in != null)
- in.close();
- }
}
/**
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java b/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
index 26c76de4bc1..20ed0fc8018 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
@@ -33,16 +33,16 @@ final class BufferedOutput {
public int position() { return pos; }
- final void put(byte b) {
+ void put(byte b) {
reserve(1);
buf[pos++] = b;
}
- final void absolutePut(int position, byte b) {
+ void absolutePut(int position, byte b) {
buf[position] = b;
}
- final void put(byte[] bytes) {
+ void put(byte[] bytes) {
reserve(bytes.length);
for (byte b : bytes) {
buf[pos++] = b;