mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-09-04 15:18:09 +00:00
Preparations for file content encryption (no partial support yet)
This commit is contained in:
@@ -24,7 +24,7 @@ public interface WritableBytes {
|
||||
|
||||
/**
|
||||
* Writes the data in the given byte buffer to this readable bytes at the
|
||||
* given position.
|
||||
* given position, overwriting existing content (not inserting).
|
||||
*
|
||||
* @param target
|
||||
* the byte buffer to use
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.cryptomator.io;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* TODO this probably doesn't belong into this maven module, but it is used by various filesystem layers.
|
||||
*/
|
||||
public final class ByteBuffers {
|
||||
|
||||
private ByteBuffers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies as many bytes as possible from the given source to the destination buffer.
|
||||
* The position of both buffers will be incremented by as many bytes as have been copied.
|
||||
*
|
||||
* @param source ByteBuffer from which bytes are read
|
||||
* @param destination ByteBuffer into which bytes are written
|
||||
* @return number of bytes copied, i.e. {@link ByteBuffer#remaining() source.remaining()} or {@link ByteBuffer#remaining() destination.remaining()}, whatever is less.
|
||||
*/
|
||||
public static int copy(ByteBuffer source, ByteBuffer destination) {
|
||||
final int numBytes = Math.min(source.remaining(), destination.remaining());
|
||||
final ByteBuffer tmp = source.asReadOnlyBuffer();
|
||||
tmp.limit(tmp.position() + numBytes);
|
||||
destination.put(tmp);
|
||||
source.position(tmp.position());
|
||||
return numBytes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.cryptomator.io;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ByteBuffersTest {
|
||||
|
||||
@Test
|
||||
public void testCopyOfEmptySource() {
|
||||
final ByteBuffer src = ByteBuffer.allocate(0);
|
||||
final ByteBuffer dst = ByteBuffer.allocate(5);
|
||||
dst.put(new byte[3]);
|
||||
Assert.assertEquals(0, src.position());
|
||||
Assert.assertEquals(0, src.remaining());
|
||||
Assert.assertEquals(3, dst.position());
|
||||
Assert.assertEquals(2, dst.remaining());
|
||||
ByteBuffers.copy(src, dst);
|
||||
Assert.assertEquals(0, src.position());
|
||||
Assert.assertEquals(0, src.remaining());
|
||||
Assert.assertEquals(3, dst.position());
|
||||
Assert.assertEquals(2, dst.remaining());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToEmptyDestination() {
|
||||
final ByteBuffer src = ByteBuffer.wrap(new byte[4]);
|
||||
final ByteBuffer dst = ByteBuffer.allocate(0);
|
||||
src.put(new byte[2]);
|
||||
Assert.assertEquals(2, src.position());
|
||||
Assert.assertEquals(2, src.remaining());
|
||||
Assert.assertEquals(0, dst.position());
|
||||
Assert.assertEquals(0, dst.remaining());
|
||||
ByteBuffers.copy(src, dst);
|
||||
Assert.assertEquals(2, src.position());
|
||||
Assert.assertEquals(2, src.remaining());
|
||||
Assert.assertEquals(0, dst.position());
|
||||
Assert.assertEquals(0, dst.remaining());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToBiggerDestination() {
|
||||
final ByteBuffer src = ByteBuffer.wrap(new byte[2]);
|
||||
final ByteBuffer dst = ByteBuffer.allocate(10);
|
||||
dst.put(new byte[3]);
|
||||
Assert.assertEquals(0, src.position());
|
||||
Assert.assertEquals(2, src.remaining());
|
||||
Assert.assertEquals(3, dst.position());
|
||||
Assert.assertEquals(7, dst.remaining());
|
||||
ByteBuffers.copy(src, dst);
|
||||
Assert.assertEquals(2, src.position());
|
||||
Assert.assertEquals(0, src.remaining());
|
||||
Assert.assertEquals(5, dst.position());
|
||||
Assert.assertEquals(5, dst.remaining());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToSmallerDestination() {
|
||||
final ByteBuffer src = ByteBuffer.wrap(new byte[5]);
|
||||
final ByteBuffer dst = ByteBuffer.allocate(2);
|
||||
Assert.assertEquals(0, src.position());
|
||||
Assert.assertEquals(5, src.remaining());
|
||||
Assert.assertEquals(0, dst.position());
|
||||
Assert.assertEquals(2, dst.remaining());
|
||||
ByteBuffers.copy(src, dst);
|
||||
Assert.assertEquals(2, src.position());
|
||||
Assert.assertEquals(3, src.remaining());
|
||||
Assert.assertEquals(2, dst.position());
|
||||
Assert.assertEquals(0, dst.remaining());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user