@@ -32,15 +32,15 @@ public <T extends IO.Writable.Seekable & IO.Readable.Seekable> OutputToInput(T i
32
32
private IO io ;
33
33
private String sourceDescription ;
34
34
private boolean eof = false ;
35
- private LockPoint <IOException > lock = new LockPoint <>();
35
+ private Async <IOException > waitForData = new Async <>();
36
36
private long writePos = 0 ;
37
37
private long readPos = 0 ;
38
38
private LockPoint <NoException > lockIO = new LockPoint <>();
39
39
40
40
@ Override
41
41
protected IAsync <IOException > closeUnderlyingResources () {
42
42
eof = true ;
43
- lock .error (new EOFException ());
43
+ waitForData .error (new EOFException ());
44
44
return io .closeAsync ();
45
45
}
46
46
@@ -74,12 +74,12 @@ public IO getWrappedIO() {
74
74
@ Override
75
75
public void endOfData () {
76
76
eof = true ;
77
- lock .error (new EOFException ());
77
+ waitForData .error (new EOFException ());
78
78
}
79
79
80
80
@ Override
81
81
public void signalErrorBeforeEndOfData (IOException error ) {
82
- lock .error (error );
82
+ waitForData .error (error );
83
83
lockIO .unlock ();
84
84
}
85
85
@@ -107,7 +107,7 @@ public int writeSync(ByteBuffer buffer) throws IOException {
107
107
nb = ((IO .Writable .Seekable )io ).writeSync (writePos , buffer );
108
108
writePos += nb ;
109
109
lockIO .unlock ();
110
- lock . unlock ();
110
+ waitForData . unblock ();
111
111
return nb ;
112
112
}
113
113
@@ -120,15 +120,15 @@ public AsyncSupplier<Integer, IOException> writeAsync(ByteBuffer buffer, Consume
120
120
if (param .getValue1 () != null ) {
121
121
writePos += param .getValue1 ().intValue ();
122
122
lockIO .unlock ();
123
- lock . unlock ();
123
+ waitForData . unblock ();
124
124
if (ondone != null ) ondone .accept (param );
125
125
} else {
126
126
lockIO .unlock ();
127
127
if (ondone != null ) ondone .accept (param );
128
- lock .error (param .getValue2 ());
128
+ waitForData .error (param .getValue2 ());
129
129
}
130
130
});
131
- write .onCancel (lock ::cancel );
131
+ write .onCancel (waitForData ::cancel );
132
132
write .forward (result );
133
133
return null ;
134
134
})).start ();
@@ -138,21 +138,25 @@ public AsyncSupplier<Integer, IOException> writeAsync(ByteBuffer buffer, Consume
138
138
@ Override
139
139
public IAsync <IOException > canStartReading () {
140
140
if (eof ) return new Async <>(true );
141
- if (lock .hasError ()) return lock ;
141
+ if (waitForData .hasError ()) return waitForData ;
142
142
if (readPos < writePos ) return new Async <>(true );
143
- return lock ;
143
+ return waitForData ;
144
144
}
145
145
146
146
@ Override
147
147
@ SuppressWarnings ("squid:S2589" ) // eof may change in a concurrent operation
148
148
public int readSync (long pos , ByteBuffer buffer ) throws IOException {
149
- if (lock .hasError () && !eof )
150
- throw new OutputToInputTransferException (lock .getError ());
149
+ if (waitForData .hasError () && !eof )
150
+ throw new OutputToInputTransferException (waitForData .getError ());
151
151
while (pos >= writePos ) {
152
152
if (eof ) return -1 ;
153
- if (lock .hasError () && !eof )
154
- throw new OutputToInputTransferException (lock .getError ());
155
- lock .lock ();
153
+ if (waitForData .hasError () && !eof )
154
+ throw new OutputToInputTransferException (waitForData .getError ());
155
+ synchronized (waitForData ) {
156
+ if (pos >= writePos && waitForData .isDone ())
157
+ waitForData .reset ();
158
+ }
159
+ waitForData .block (0 );
156
160
}
157
161
int nb ;
158
162
lockIO .lock ();
@@ -194,8 +198,8 @@ public AsyncSupplier<Integer, IOException> readAsync(ByteBuffer buffer, Consumer
194
198
@ Override
195
199
@ SuppressWarnings ("squid:S2589" ) // may change in a concurrent operation
196
200
public AsyncSupplier <Integer , IOException > readAsync (long pos , ByteBuffer buffer , Consumer <Pair <Integer ,IOException >> ondone ) {
197
- if (lock .hasError () && !eof ) {
198
- IOException e = new OutputToInputTransferException (lock .getError ());
201
+ if (waitForData .hasError () && !eof ) {
202
+ IOException e = new OutputToInputTransferException (waitForData .getError ());
199
203
if (ondone != null ) ondone .accept (new Pair <>(null , e ));
200
204
return new AsyncSupplier <>(null , e );
201
205
}
@@ -205,7 +209,11 @@ public AsyncSupplier<Integer, IOException> readAsync(long pos, ByteBuffer buffer
205
209
return new AsyncSupplier <>(Integer .valueOf (-1 ), null );
206
210
}
207
211
AsyncSupplier <Integer , IOException > result = new AsyncSupplier <>();
208
- lock .thenStart (operation (
212
+ synchronized (waitForData ) {
213
+ if (pos >= writePos && waitForData .isDone ())
214
+ waitForData .reset ();
215
+ }
216
+ waitForData .thenStart (operation (
209
217
taskSyncToAsync ("OutputToInput.readAsync" , result , ondone , () -> Integer .valueOf (readSync (pos , buffer )))), true );
210
218
return result ;
211
219
}
@@ -263,17 +271,21 @@ public long skipSync(long n) throws IOException {
263
271
return n ;
264
272
}
265
273
if (readPos + n > writePos ) {
266
- if (lock .hasError () && !eof )
267
- throw new OutputToInputTransferException (lock .getError ());
274
+ if (waitForData .hasError () && !eof )
275
+ throw new OutputToInputTransferException (waitForData .getError ());
268
276
while (readPos + n > writePos ) {
269
277
if (eof ) {
270
278
n = writePos - readPos ;
271
279
readPos = writePos ;
272
280
return n ;
273
281
}
274
- if (lock .hasError () && !eof )
275
- throw new OutputToInputTransferException (lock .getError ());
276
- lock .lock ();
282
+ if (waitForData .hasError () && !eof )
283
+ throw new OutputToInputTransferException (waitForData .getError ());
284
+ synchronized (waitForData ) {
285
+ if (readPos + n > writePos && waitForData .isDone ())
286
+ waitForData .reset ();
287
+ }
288
+ waitForData .block (0 );
277
289
}
278
290
}
279
291
readPos += n ;
@@ -300,7 +312,11 @@ public AsyncSupplier<Long, IOException> skipAsync(long n, Consumer<Pair<Long,IOE
300
312
return new AsyncSupplier <>(Long .valueOf (m ), null );
301
313
}
302
314
AsyncSupplier <Long , IOException > result = new AsyncSupplier <>();
303
- lock .thenStart (operation (taskSyncToAsync ("OutputToInput.skipAsync" , result , ondone , () -> Long .valueOf (skipSync (n )))), true );
315
+ synchronized (waitForData ) {
316
+ if (readPos + n > writePos && waitForData .isDone ())
317
+ waitForData .reset ();
318
+ }
319
+ waitForData .thenStart (operation (taskSyncToAsync ("OutputToInput.skipAsync" , result , ondone , () -> Long .valueOf (skipSync (n )))), true );
304
320
return result ;
305
321
}
306
322
@@ -320,19 +336,24 @@ public long seekSync(SeekType type, long move) throws IOException {
320
336
skipSync (move );
321
337
return readPos ;
322
338
default : //case FROM_END:
323
- while (!eof && !lock .hasError ()) {
324
- lock .lock ();
339
+ while (!eof && !waitForData .hasError ()) {
340
+ synchronized (waitForData ) {
341
+ if (!eof && waitForData .isDone ())
342
+ waitForData .reset ();
343
+ }
344
+ waitForData .block (0 );
325
345
}
326
346
if (eof ) {
327
347
readPos = writePos ;
328
348
skipSync (-move );
329
349
return readPos ;
330
350
}
331
- throw new OutputToInputTransferException (lock .getError ());
351
+ throw new OutputToInputTransferException (waitForData .getError ());
332
352
}
333
353
}
334
354
335
355
@ Override
356
+ @ SuppressWarnings ("java:S3776" ) // complexity
336
357
public AsyncSupplier <Long , IOException > seekAsync (SeekType type , long move , Consumer <Pair <Long ,IOException >> ondone ) {
337
358
AsyncSupplier <Long , IOException > res = new AsyncSupplier <>();
338
359
switch (type ) {
@@ -350,8 +371,8 @@ public AsyncSupplier<Long, IOException> seekAsync(SeekType type, long move, Cons
350
371
}, res );
351
372
return res ;
352
373
case FROM_END :
353
- if (lock .hasError () && !eof )
354
- return IOUtil .error (new OutputToInputTransferException (lock .getError ()), ondone );
374
+ if (waitForData .hasError () && !eof )
375
+ return IOUtil .error (new OutputToInputTransferException (waitForData .getError ()), ondone );
355
376
if (eof ) {
356
377
if (move <= 0 )
357
378
readPos = writePos ;
@@ -361,7 +382,11 @@ public AsyncSupplier<Long, IOException> seekAsync(SeekType type, long move, Cons
361
382
return IOUtil .success (Long .valueOf (readPos ), ondone );
362
383
}
363
384
AsyncSupplier <Long , IOException > result = new AsyncSupplier <>();
364
- lock .thenStart (operation (Task .cpu ("OutputToInput.seekAsync" , io .getPriority (), t -> {
385
+ synchronized (waitForData ) {
386
+ if (!eof && waitForData .isDone ())
387
+ waitForData .reset ();
388
+ }
389
+ waitForData .thenStart (operation (Task .cpu ("OutputToInput.seekAsync" , io .getPriority (), t -> {
365
390
try {
366
391
Long nb = Long .valueOf (seekSync (type , move ));
367
392
if (ondone != null ) ondone .accept (new Pair <>(nb , null ));
0 commit comments