Skip to content

Commit a97a363

Browse files
committed
fix OutputToInput
1 parent 339662d commit a97a363

File tree

6 files changed

+26
-149
lines changed

6 files changed

+26
-149
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,9 +156,11 @@ public int readSync(long pos, ByteBuffer buffer) throws IOException {
156156
}
157157
int nb;
158158
lockIO.lock();
159+
int lim = buffer.limit();
159160
if (writePos - pos < buffer.remaining())
160161
buffer.limit(buffer.position() + (int)(writePos - pos));
161162
nb = ((IO.Readable.Seekable)io).readSync(pos, buffer);
163+
buffer.limit(lim);
162164
lockIO.unlock();
163165
return nb;
164166
}
@@ -210,10 +212,12 @@ public AsyncSupplier<Integer, IOException> readAsync(long pos, ByteBuffer buffer
210212
AsyncSupplier<Integer, IOException> result = new AsyncSupplier<>();
211213
Task.cpu("OutputToInput.readAsync", io.getPriority(), t -> {
212214
lockIO.lock();
215+
int lim = buffer.limit();
213216
if (writePos - pos < buffer.remaining())
214217
buffer.limit(buffer.position() + (int)(writePos - pos));
215218
AsyncSupplier<Integer, IOException> read = ((IO.Readable.Seekable)io).readAsync(pos, buffer, ondone);
216219
read.onDone(() -> {
220+
buffer.limit(lim);
217221
lockIO.unlock();
218222
read.forward(result);
219223
});

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package net.lecousin.framework.core.tests.io.o2i;
22

3+
import java.io.File;
4+
import java.io.IOException;
35
import java.util.Collection;
46

7+
import net.lecousin.framework.concurrent.threads.Task;
58
import net.lecousin.framework.concurrent.threads.Task.Priority;
69
import net.lecousin.framework.core.test.io.TestIO;
710
import net.lecousin.framework.core.test.runners.LCConcurrentRunner;
11+
import net.lecousin.framework.io.FileIO;
812
import net.lecousin.framework.io.IO;
13+
import net.lecousin.framework.io.buffering.BufferedIO;
914
import net.lecousin.framework.io.buffering.ByteArrayIO;
1015
import net.lecousin.framework.io.buffering.IOInMemoryOrFile;
16+
import net.lecousin.framework.io.buffering.MemoryIO;
1117
import net.lecousin.framework.io.out2in.OutputToInput;
1218

1319
import org.junit.runner.RunWith;
@@ -18,7 +24,7 @@ public class TestOutputToInput extends net.lecousin.framework.core.test.io.TestO
1824

1925
@Parameters(name = "nbBuf = {1}, testCase = {2}")
2026
public static Collection<Object[]> parameters() {
21-
return addTestParameter(TestIO.UsingTestData.generateTestCases(true), Integer.valueOf(1), Integer.valueOf(2));
27+
return addTestParameter(TestIO.UsingTestData.generateTestCases(true), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5));
2228
}
2329

2430
public TestOutputToInput(byte[] testBuf, int nbBuf, int testCase) {
@@ -29,11 +35,24 @@ public TestOutputToInput(byte[] testBuf, int nbBuf, int testCase) {
2935
private int testCase;
3036

3137
@Override
32-
protected IO.OutputToInput createOutputToInput() {
38+
protected IO.OutputToInput createOutputToInput() throws IOException {
3339
switch (testCase) {
3440
default:
35-
case 1: return new OutputToInput(new IOInMemoryOrFile(4096, Priority.NORMAL, "test"), "test");
41+
case 1: return new OutputToInput(new IOInMemoryOrFile(1 * 1024 * 1024, Priority.NORMAL, "test"), "test");
3642
case 2: return new OutputToInput(new ByteArrayIO(new byte[nbBuf * testBuf.length + 1024], "test"), "test");
43+
case 3: return new OutputToInput(new MemoryIO(4096, "test"), "test");
44+
case 4: {
45+
File file = File.createTempFile("test", "outputtoinput");
46+
file.deleteOnExit();
47+
FileIO.ReadWrite fio = new FileIO.ReadWrite(file, Task.Priority.NORMAL);
48+
BufferedIO.ReadWrite bio = new BufferedIO.ReadWrite(fio, 0, 4096, 4096, false);
49+
return new OutputToInput(bio, "test");
50+
}
51+
case 5: {
52+
File file = File.createTempFile("test", "outputtoinput");
53+
file.deleteOnExit();
54+
return new OutputToInput(new FileIO.ReadWrite(file, Task.Priority.NORMAL), "test");
55+
}
3756
}
3857
}
3958

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)