Skip to content

Commit a2707c6

Browse files
committed
BlockChannel 短縮に対応
1 parent 5a08d4f commit a2707c6

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

src/main/java/net/siisise/block/BlockChannel.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,25 @@ public long size() throws IOException {
6464
}
6565

6666
/**
67-
* 詰める subブロックで茶を濁す
67+
* 詰める.
68+
* EditBlock は詰め.
69+
* OverBlock の場合は subブロックで茶を濁す
6870
* @param size 全体のサイズ
6971
* @return 縮めたブロック
7072
* @throws IOException
7173
*/
7274
@Override
7375
public SeekableByteChannel truncate(long size) throws IOException {
74-
long p = block.backLength();
75-
block = block.sub(0, size);
76-
block.seek(p);
76+
if ( block.backLength() + block.length() > size ) {
77+
long p = Math.min(block.backLength(), size);
78+
if ( block instanceof EditBlock ) {
79+
((EditBlock)block).del(size, block.backLength() + block.length() - size);
80+
} else {
81+
block = block.sub(0, size);
82+
}
83+
block.seek(p);
84+
}
7785
return this;
78-
// return new BlockChannel(block.sub(0, size));
7986
}
8087

8188
@Override

src/main/java/net/siisise/block/EditBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface EditBlock extends OverBlock, IndexEdit {
2828

2929
/**
3030
* 切り取る.
31-
* 編集可能な場合のみ、不要なサイズを切り取る.
31+
* 不要なサイズを切り取る.
3232
* del と類似.
3333
*
3434
* @param length 長さ

src/test/java/net/siisise/block/ByteBlockTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class ByteBlockTest {
2626

2727
public ByteBlockTest() {
2828
}
29+
30+
ByteBlock block(byte[] src) {
31+
return new ByteBlock(src);
32+
}
2933

3034
/**
3135
* Test of readBlock method, of class ByteBlock.
@@ -34,7 +38,7 @@ public ByteBlockTest() {
3438
public void testReadBlock() {
3539
System.out.println("readBlock");
3640
byte[] ss = "utca".getBytes(StandardCharsets.UTF_8);
37-
ByteBlock block = new ByteBlock(ss);
41+
ByteBlock block = block(ss);
3842
byte[] rd = new byte[1];
3943

4044
assertEquals(4,block.size());

src/test/java/net/siisise/block/PacketBlockTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
*/
1616
package net.siisise.block;
1717

18+
import net.siisise.io.IndexEditTest;
1819
import net.siisise.io.PacketA;
1920
import org.junit.jupiter.api.Test;
2021
import static org.junit.jupiter.api.Assertions.*;
2122

2223
/**
2324
*
2425
*/
25-
public class PacketBlockTest {
26+
public class PacketBlockTest extends IndexEditTest {
2627

2728
public PacketBlockTest() {
2829
}
30+
31+
PacketBlock block(byte[] src) {
32+
return new PacketBlock(src);
33+
}
2934

3035
/**
3136
* Test of skip method, of class PacketBlock.
@@ -42,7 +47,7 @@ public void testSkip() {
4247
PacketA b = p.readPacket(length);
4348
assertEquals(expResult, b.size());
4449

45-
PacketBlock instance = new PacketBlock(data);
50+
PacketBlock instance = block(data);
4651
assertEquals(10, instance.length());
4752
long result = instance.skip(length);
4853
assertEquals(expResult, result);

src/test/java/net/siisise/io/IndexEditTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class IndexEditTest {
2727

2828
public IndexEditTest() {
2929
}
30+
31+
EditBlock block(byte[] ar) {
32+
return new SinglePacketBlock(ar);
33+
}
3034

3135
/**
3236
* Test of del method, of class IndexEdit.
@@ -35,7 +39,7 @@ public IndexEditTest() {
3539
public void testDel_long() {
3640
System.out.println("del");
3741
long index = 2L;
38-
IndexEdit instance = new SinglePacketBlock(new byte[] {6,5,4,3,2,1});
42+
IndexEdit instance = block(new byte[] {6,5,4,3,2,1});
3943
byte expResult = 4;
4044
byte result = instance.del(index);
4145
assertEquals(expResult, result);
@@ -49,7 +53,7 @@ public void testDel_long_long() {
4953
System.out.println("del");
5054
long index = 2L;
5155
long size = 3L;
52-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
56+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
5357
instance.del(index, size);
5458
assertEquals(0, instance.backLength() );
5559
assertEquals(3, instance.length() );
@@ -65,7 +69,7 @@ public void testDel_long_byteArr() {
6569
System.out.println("del");
6670
long index = 1L;
6771
byte[] buf = new byte[3];
68-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
72+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
6973
byte[] expResult = new byte[] {6,7,8};
7074
EditBlock result = instance.del(index, buf);
7175
assertArrayEquals(expResult, buf);
@@ -82,7 +86,7 @@ public void testDel_4args() {
8286
byte[] buf = new byte[4];
8387
int offset = 1;
8488
int length = 2;
85-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
89+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
8690
byte[] expResult = new byte[] {0,8,9,0};
8791
EditBlock result = instance.del(index, buf, offset, length);
8892
assertEquals(result, instance);
@@ -98,7 +102,7 @@ public void testAdd_long_byte() {
98102
long index = 2L;
99103
byte src = 11;
100104
byte[] exr = new byte[] {5,6,11,7,8,9,10};
101-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
105+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
102106
instance.add(index, src);
103107
assertArrayEquals(exr, instance.toByteArray());
104108
}
@@ -112,7 +116,7 @@ public void testAdd_long_byteArr() {
112116
long index = 4L;
113117
byte[] src = new byte[] {0,1,2};
114118
byte[] exr = new byte[] {5,6,7,8,0,1,2,9,10};
115-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
119+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
116120
instance.add(index, src);
117121
assertArrayEquals(exr, instance.toByteArray());
118122
}
@@ -127,7 +131,7 @@ public void testAdd_4args() {
127131
byte[] src = new byte[] {9,8,7,6};
128132
int offset = 1;
129133
int length = 2;
130-
EditBlock instance = new SinglePacketBlock(new byte[] {5,6,7,8,9,10});
134+
EditBlock instance = block(new byte[] {5,6,7,8,9,10});
131135
instance.add(index, src, offset, length);
132136
byte[] exr = new byte[] {5,6,7,8,8,7,9,10};
133137
assertArrayEquals(exr, instance.toByteArray());

0 commit comments

Comments
 (0)