Skip to content

Commit 339662d

Browse files
committed
fix OutputToInput + add tests
1 parent 9488953 commit 339662d

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/io/out2in/OutputToInput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public int readSync(long pos, ByteBuffer buffer) throws IOException {
156156
}
157157
int nb;
158158
lockIO.lock();
159+
if (writePos - pos < buffer.remaining())
160+
buffer.limit(buffer.position() + (int)(writePos - pos));
159161
nb = ((IO.Readable.Seekable)io).readSync(pos, buffer);
160162
lockIO.unlock();
161163
return nb;
@@ -208,6 +210,8 @@ public AsyncSupplier<Integer, IOException> readAsync(long pos, ByteBuffer buffer
208210
AsyncSupplier<Integer, IOException> result = new AsyncSupplier<>();
209211
Task.cpu("OutputToInput.readAsync", io.getPriority(), t -> {
210212
lockIO.lock();
213+
if (writePos - pos < buffer.remaining())
214+
buffer.limit(buffer.position() + (int)(writePos - pos));
211215
AsyncSupplier<Integer, IOException> read = ((IO.Readable.Seekable)io).readAsync(pos, buffer, ondone);
212216
read.onDone(() -> {
213217
lockIO.unlock();

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestOutputToInput.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public void testWriteSyncReadHalfSync() throws Exception {
9191
readHalfSync(o2i, (nbRead % 2) == 0, b);
9292
nbRead++;
9393
}
94-
if (o2i.readSync(ByteBuffer.wrap(new byte[1])) == 1)
94+
if (o2i.readSync(ByteBuffer.wrap(new byte[1])) > 0)
95+
throw new IOException("Data can be read after the end");
96+
if (o2i.readAsync(ByteBuffer.wrap(new byte[1])).blockResult(0).intValue() > 0)
9597
throw new IOException("Data can be read after the end");
9698
}
9799
}
@@ -195,6 +197,37 @@ public void run() {
195197
}
196198
}
197199

200+
@Test
201+
public void testWriteReadMore() throws Exception {
202+
Assume.assumeTrue(nbBuf > 0);
203+
try (IO.OutputToInput o2i = createOutputToInput()) {
204+
int nbWrite = 0;
205+
while (nbWrite < nbBuf) {
206+
ByteBuffer bu = ByteBuffer.wrap(testBuf);
207+
o2i.writeSync(bu);
208+
Assert.assertEquals(0, bu.remaining());
209+
nbWrite++;
210+
}
211+
Assert.assertFalse(o2i.isFullDataAvailable());
212+
o2i.endOfData();
213+
Assert.assertTrue(o2i.isFullDataAvailable());
214+
Assert.assertEquals(nbBuf * testBuf.length, o2i.readFullySync(ByteBuffer.allocate(nbBuf * testBuf.length + 1024)));
215+
}
216+
try (IO.OutputToInput o2i = createOutputToInput()) {
217+
int nbWrite = 0;
218+
while (nbWrite < nbBuf) {
219+
ByteBuffer bu = ByteBuffer.wrap(testBuf);
220+
o2i.writeSync(bu);
221+
Assert.assertEquals(0, bu.remaining());
222+
nbWrite++;
223+
}
224+
Assert.assertFalse(o2i.isFullDataAvailable());
225+
o2i.endOfData();
226+
Assert.assertTrue(o2i.isFullDataAvailable());
227+
Assert.assertEquals(nbBuf * testBuf.length, o2i.readFullyAsync(ByteBuffer.allocate(nbBuf * testBuf.length + 1024)).blockResult(0).intValue());
228+
}
229+
}
230+
198231
@Test
199232
public void testSkipSync() throws Exception {
200233
try (IO.OutputToInput o2i = createOutputToInput()) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.lecousin.framework.core.tests.io.o2i;
2+
3+
import java.util.Collection;
4+
5+
import net.lecousin.framework.concurrent.threads.Task.Priority;
6+
import net.lecousin.framework.core.test.io.TestIO;
7+
import net.lecousin.framework.core.test.runners.LCConcurrentRunner;
8+
import net.lecousin.framework.io.IO;
9+
import net.lecousin.framework.io.buffering.ByteArrayIO;
10+
import net.lecousin.framework.io.buffering.IOInMemoryOrFile;
11+
import net.lecousin.framework.io.out2in.OutputToInput;
12+
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.Parameterized.Parameters;
15+
16+
@RunWith(LCConcurrentRunner.Parameterized.class) @org.junit.runners.Parameterized.UseParametersRunnerFactory(LCConcurrentRunner.ConcurrentParameterizedRunnedFactory.class)
17+
public class TestOutputToInput extends net.lecousin.framework.core.test.io.TestOutputToInput {
18+
19+
@Parameters(name = "nbBuf = {1}, testCase = {2}")
20+
public static Collection<Object[]> parameters() {
21+
return addTestParameter(TestIO.UsingTestData.generateTestCases(true), Integer.valueOf(1), Integer.valueOf(2));
22+
}
23+
24+
public TestOutputToInput(byte[] testBuf, int nbBuf, int testCase) {
25+
super(testBuf, nbBuf);
26+
this.testCase = testCase;
27+
}
28+
29+
private int testCase;
30+
31+
@Override
32+
protected IO.OutputToInput createOutputToInput() {
33+
switch (testCase) {
34+
default:
35+
case 1: return new OutputToInput(new IOInMemoryOrFile(4096, Priority.NORMAL, "test"), "test");
36+
case 2: return new OutputToInput(new ByteArrayIO(new byte[nbBuf * testBuf.length + 1024], "test"), "test");
37+
}
38+
}
39+
40+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/io/o2i/TestOutputToInputAsReadable.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public TestOutputToInputAsReadable(File testFile, byte[] testBuf, int nbBuf) {
2828
super(testFile, testBuf, nbBuf);
2929
}
3030

31-
@SuppressWarnings("resource")
3231
@Override
3332
protected IO.Readable createReadableFromFile(FileIO.ReadOnly file, long fileSize) throws Exception {
3433
file.close();

0 commit comments

Comments
 (0)