Object (@NotNull CharSequence string) { int length = string.length(); if (length < STRING_COMPRESSION_THRESHOLD) { if (string instanceof CharBuffer && ((CharBuffer)string).capacity() > STRING_COMPRESSION_THRESHOLD) { string = string.toString(); // shrink to size } return string; } try { BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(length); @NotNull DataOutput out = new DataOutputStream(bytes); for (int i=0; i< length;i++) { char c = string.charAt(i); DataInputOutputUtilRt.writeINT(out, c); } LZ4Compressor compressor = compressor(); int bytesWritten = bytes.size(); ByteBuffer dest = ByteBuffer.wrap(spareBufferLocal.getBuffer(compressor.maxCompressedLength(bytesWritten) + 10)); DataInputOutputUtilRt.writeINT(dest, length); DataInputOutputUtilRt.writeINT(dest, bytesWritten - length); compressor.compress(ByteBuffer.wrap(bytes.getInternalBuffer(), 0, bytesWritten), dest); return dest.position() < length * 2 ? Arrays.copyOf(dest.array(), dest.position()) : string; } catch (IOException e) { e.printStackTrace(); return string; } }