Skip to content

Commit

Permalink
Use generic java.nio.files interfaces in FileSystemStorage #21
Browse files Browse the repository at this point in the history
  • Loading branch information
atomashpolskiy committed Aug 10, 2017
1 parent 6c3b13d commit b14f7b3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions bt-core/src/main/java/bt/data/file/FileSystemStorageUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public synchronized void readBlock(ByteBuffer buffer, long offset) {

try {
sbc.position(offset);
sbc.read(buffer);
int read = 1;
while (buffer.hasRemaining() && read > 0) {
read = sbc.read(buffer);
}

} catch (IOException e) {
throw new BtException("Failed to read bytes (offset: " + offset +
Expand All @@ -115,9 +118,12 @@ public synchronized byte[] readBlock(long offset, int length) {

try {
sbc.position(offset);
byte[] block = new byte[length];
sbc.read(ByteBuffer.wrap(block));
return block;
ByteBuffer buf = ByteBuffer.allocate(length);
int read = 1;
while(buf.hasRemaining() && read > 0) {
read = sbc.read(buf);
}
return buf.array();

} catch (IOException e) {
throw new BtException("Failed to read bytes (offset: " + offset +
Expand All @@ -141,7 +147,10 @@ public synchronized void writeBlock(ByteBuffer buffer, long offset) {

try {
sbc.position(offset);
sbc.write(buffer);
int written = 1;
while (buffer.hasRemaining() && written > 0) {
written = sbc.write(buffer);
}

} catch (IOException e) {
throw new BtException("Failed to write bytes (offset: " + offset +
Expand All @@ -165,7 +174,11 @@ public synchronized void writeBlock(byte[] block, long offset) {

try {
sbc.position(offset);
sbc.write(ByteBuffer.wrap(block));
ByteBuffer buf = ByteBuffer.wrap(block);
int written = 1;
while (buf.hasRemaining() && written > 0) {
written = sbc.write(buf);
}

} catch (IOException e) {
throw new BtException("Failed to write bytes (offset: " + offset +
Expand Down

0 comments on commit b14f7b3

Please sign in to comment.