@@ -338,51 +338,10 @@ private void readSegments(int totalWords, int segmentCount, int segment0Size, in
338338 abstract void read (int bytes , Consumer <? super ByteBuffer > consumer );
339339 }
340340
341- static class AsyncSocketReader extends AsyncMessageReader {
342- private final AsynchronousSocketChannel channel ;
343- private final long timeout ;
344- private final TimeUnit timeUnit ;
345-
346- AsyncSocketReader (AsynchronousSocketChannel channel , ReaderOptions options , long timeout , TimeUnit timeUnit ) {
347- super (options );
348- this .channel = channel ;
349- this .timeout = timeout ;
350- this .timeUnit = timeUnit ;
351- }
352-
353- void read (int bytes , Consumer <? super ByteBuffer > consumer ) {
354- var buffer = Serialize .makeByteBuffer (bytes );
355- var handler = new CompletionHandler <Integer , Object >() {
356- @ Override
357- public void completed (Integer result , Object attachment ) {
358- //System.out.println(channel.toString() + ": read " + result + " bytes");
359- if (result <= 0 ) {
360- var text = result == 0
361- ? "Read zero bytes. Is the channel in non-blocking mode?"
362- : "Premature EOF" ;
363- readCompleted .completeExceptionally (new IOException (text ));
364- } else if (buffer .hasRemaining ()) {
365- // partial read
366- channel .read (buffer , timeout , timeUnit , null , this );
367- } else {
368- consumer .accept (buffer );
369- }
370- }
371-
372- @ Override
373- public void failed (Throwable exc , Object attachment ) {
374- readCompleted .completeExceptionally (exc );
375- }
376- };
377-
378- this .channel .read (buffer , this .timeout , this .timeUnit , null , handler );
379- }
380- }
381-
382- static class AsyncByteChannelReader extends AsyncMessageReader {
341+ static class AsynchronousByteChannelReader extends AsyncMessageReader {
383342 private final AsynchronousByteChannel channel ;
384343
385- AsyncByteChannelReader (AsynchronousByteChannel channel , ReaderOptions options ) {
344+ AsynchronousByteChannelReader (AsynchronousByteChannel channel , ReaderOptions options ) {
386345 super (options );
387346 this .channel = channel ;
388347 }
@@ -421,23 +380,7 @@ public static CompletableFuture<MessageReader> readAsync(AsynchronousByteChannel
421380 }
422381
423382 public static CompletableFuture <MessageReader > readAsync (AsynchronousByteChannel channel , ReaderOptions options ) {
424- return new AsyncByteChannelReader (channel , options ).getMessage ();
425- }
426-
427- public static CompletableFuture <MessageReader > readAsync (AsynchronousSocketChannel channel ) {
428- return readAsync (channel , ReaderOptions .DEFAULT_READER_OPTIONS , Long .MAX_VALUE , TimeUnit .SECONDS );
429- }
430-
431- public static CompletableFuture <MessageReader > readAsync (AsynchronousSocketChannel channel , ReaderOptions options ) {
432- return readAsync (channel , options , Long .MAX_VALUE , TimeUnit .SECONDS );
433- }
434-
435- public static CompletableFuture <MessageReader > readAsync (AsynchronousSocketChannel channel , long timeout , TimeUnit timeUnit ) {
436- return readAsync (channel , ReaderOptions .DEFAULT_READER_OPTIONS , timeout , timeUnit );
437- }
438-
439- public static CompletableFuture <MessageReader > readAsync (AsynchronousSocketChannel channel , ReaderOptions options , long timeout , TimeUnit timeUnit ) {
440- return new AsyncSocketReader (channel , options , timeout , timeUnit ).getMessage ();
383+ return new AsynchronousByteChannelReader (channel , options ).getMessage ();
441384 }
442385
443386 public static CompletableFuture <java .lang .Void > writeAsync (AsynchronousByteChannel outputChannel , MessageBuilder message ) {
@@ -477,50 +420,6 @@ public void failed(Throwable exc, Integer attachment) {
477420 return writeCompleted ;
478421 }
479422
480- public static CompletableFuture <java .lang .Void > writeAsync (AsynchronousSocketChannel outputChannel , MessageBuilder message ) {
481- return writeAsync (outputChannel , message , Long .MAX_VALUE , TimeUnit .SECONDS );
482- }
483-
484- public static CompletableFuture <java .lang .Void > writeAsync (AsynchronousSocketChannel outputChannel , MessageBuilder message , long timeout , TimeUnit timeUnit ) {
485- var writeCompleted = new CompletableFuture <java .lang .Void >();
486- var segments = message .getSegmentsForOutput ();
487- var header = getHeaderForOutput (segments );
488- long totalBytes = header .remaining ();
489-
490- // TODO avoid this copy?
491- var allSegments = new ByteBuffer [segments .length +1 ];
492- allSegments [0 ] = header ;
493- for (int ii = 0 ; ii < segments .length ; ++ii ) {
494- var segment = segments [ii ];
495- allSegments [ii +1 ] = segment ;
496- totalBytes += segment .remaining ();
497- }
498-
499- outputChannel .write (allSegments , 0 , allSegments .length , timeout , timeUnit , totalBytes , new CompletionHandler <>() {
500- @ Override
501- public void completed (Long result , Long totalBytes ) {
502- //System.out.println(outputChannel.toString() + ": Wrote " + result + "/" + totalBytes + " bytes");
503- if (result < 0 ) {
504- writeCompleted .completeExceptionally (new IOException ("Write failed" ));
505- }
506- else if (result < totalBytes ) {
507- // partial write
508- outputChannel .write (allSegments , 0 , allSegments .length , timeout , timeUnit , totalBytes - result , this );
509- }
510- else {
511- writeCompleted .complete (null );
512- }
513- }
514-
515- @ Override
516- public void failed (Throwable exc , Long attachment ) {
517- writeCompleted .completeExceptionally (exc );
518- }
519- });
520-
521- return writeCompleted ;
522- }
523-
524423 private static ByteBuffer getHeaderForOutput (ByteBuffer [] segments ) {
525424 assert segments .length > 0 : "Empty message" ;
526425 int tableSize = (segments .length + 2 ) & (~1 );
0 commit comments