Package org.xsocket.connection

Examples of org.xsocket.connection.IWriteCompletionHandler


  public void forwardTo(final BodyDataSink bodyDataSink, final IBodyCompleteListener completeListener) throws IOException {
   
    StringBuilder sb = new StringBuilder();
    writeHeadersTo(sb);

    IWriteCompletionHandler ch = new IWriteCompletionHandler() {
     
      @Execution(Execution.NONTHREADED)
      public void onWritten(int written) throws IOException {
        dataSource.forwardTo(bodyDataSink, completeListener);
      }
View Full Code Here


            final FutureResponseHandler respHdl = new FutureResponseHandler();
            final BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
            dataSink.setFlushmode(FlushMode.ASYNC);
           
           
            IWriteCompletionHandler completionHandler1 = new IWriteCompletionHandler() {
               
                public void onWritten(int written) throws IOException {
                    try {
                        IHttpResponse response = respHdl.getResponse();
                        Assert.assertEquals(200, response.getStatus());
                       
                        final BodyDataSource dataSource = response.getBody();
                        Assert.assertEquals("test", dataSource.readStringByLength(4));
                       
   
                        IWriteCompletionHandler completionHandler2 = new IWriteCompletionHandler() {
                           
                            public void onWritten(int written) throws IOException {
                                Assert.assertEquals("12345", dataSource.readStringByLength(5));

                                IWriteCompletionHandler completionHandler3 = new IWriteCompletionHandler() {
                                   
                                    public void onWritten(int written) throws IOException {
                                        Assert.assertEquals("789", dataSource.readStringByLength(3));
                                        dataSink.close();
                                    }
View Full Code Here

          if (LOG.isLoggable(Level.FINE)) {
              LOG.fine("[" + getId() + "] writing part " + part);
          }

         
          IWriteCompletionHandler ch = new IWriteCompletionHandler() {
               
              @Execution(Execution.NONTHREADED)
                public void onWritten(int written) throws IOException {
                   
                    IBodyCompleteListener cl = new IBodyCompleteListener() {
                       
                        public void onComplete() throws IOException {
                           
                            if (successor == null) {
                                if (LOG.isLoggable(Level.FINE)) {
                                    LOG.fine("[" + getId() + "] writing close delimiter");
                                }
                               
                                IWriteCompletionHandler ch2 = new IWriteCompletionHandler() {
                                   
                                    @Execution(Execution.NONTHREADED)
                                    public void onWritten(int written) throws IOException {
                                        dataSink.closeQuitly();
                                        MultipartDataSource.this.closeQuitly();
View Full Code Here

           
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final FileChannel fc = raf.getChannel();
           
           
            IWriteCompletionHandler sendFileProcess = new IWriteCompletionHandler() {
               
                private long remaining = file.length();
                private long offset = 0;
                private long length = 0;
               
               
                public void onWritten(int written) throws IOException {
                    write();
                }
               
               
                private void write() throws IOException {
                   
                    // remaining data to write?
                    if (remaining > 0) {
                       
                        // limit the buffer allocation size
                        if (remaining > 4096) {
                            length = 4096;
                        } else {
                            length = remaining;
                        }
                       
                        MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, offset, length);
                        ByteBuffer[] bufs = new ByteBuffer[] { buffer };
                 
                        outChannel.write(bufs, this);
                           
                        offset += length;
                        remaining -= length;

                    // no, closing channel
                    } else {
                        closeFile();
                        outChannel.close();
                    }
                }
               
                public void onException(IOException ioe) {
                    closeFile();
                    outChannel.destroy();
                }
               
               
                private void closeFile() {
                    try {
                        fc.close();
                        raf.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            };
           
            sendFileProcess.onWritten(0)// start sending
        }
View Full Code Here

           
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final FileChannel fc = raf.getChannel();
           
           
            IWriteCompletionHandler sendFileProcess = new IWriteCompletionHandler() {
               
                private long remaining = file.length();
                private long offset = 0;
                private long length = 0;
               
               
                public void onWritten(int written) throws IOException {
                    write();
                }
               
               
                private void write() throws IOException {
                   
                    // remaining data to write?
                    if (remaining > 0) {
                       
                        // limit the buffer allocation size
                        if (remaining > 4096) {
                            length = 4096;
                        } else {
                            length = remaining;
                        }
                       
                        MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, offset, length);
                        ByteBuffer[] bufs = new ByteBuffer[] { buffer };
                 
                        outChannel.write(bufs, this);
                           
                        offset += length;
                        remaining -= length;

                    // no, closing channel
                    } else {
                        closeFile();
                        outChannel.close();
                    }
                }
               
                public void onException(IOException ioe) {
                    closeFile();
                    outChannel.destroy();
                }
               
               
                private void closeFile() {
                    try {
                        fc.close();
                        raf.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            };
           
            sendFileProcess.onWritten(0)// start sending
        }
View Full Code Here

          if (LOG.isLoggable(Level.FINE)) {
              LOG.fine("[" + getId() + "] writing part " + part);
          }

         
          IWriteCompletionHandler ch = new IWriteCompletionHandler() {
               
              @Execution(Execution.NONTHREADED)
                public void onWritten(int written) throws IOException {
                   
                    IBodyCompleteListener cl = new IBodyCompleteListener() {
                       
                        public void onComplete() throws IOException {
                           
                            if (successor == null) {
                                if (LOG.isLoggable(Level.FINE)) {
                                    LOG.fine("[" + getId() + "] writing close delimiter");
                                }
                               
                                IWriteCompletionHandler ch2 = new IWriteCompletionHandler() {
                                   
                                    @Execution(Execution.NONTHREADED)
                                    public void onWritten(int written) throws IOException {
                                        dataSink.closeQuitly();
                                        MultipartDataSource.this.closeQuitly();
View Full Code Here

           
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final FileChannel fc = raf.getChannel();
           
           
            IWriteCompletionHandler sendFileProcess = new IWriteCompletionHandler() {
               
                private long remaining = file.length();
                private long offset = 0;
                private long length = 0;
               
               
                public void onWritten(int written) throws IOException {
                    write();
                }
               
               
                private void write() throws IOException {
                   
                    // remaining data to write?
                    if (remaining > 0) {
                       
                        // limit the buffer allocation size
                        if (remaining > 4096) {
                            length = 4096;
                        } else {
                            length = remaining;
                        }
                       
                        MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, offset, length);
                        ByteBuffer[] bufs = new ByteBuffer[] { buffer };
                 
                        outChannel.write(bufs, this);
                           
                        offset += length;
                        remaining -= length;

                    // no, closing channel
                    } else {
                        closeFile();
                        outChannel.close();
                    }
                }
               
                public void onException(IOException ioe) {
                    closeFile();
                    outChannel.destroy();
                }
               
               
                private void closeFile() {
                    try {
                        fc.close();
                        raf.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            };
           
            sendFileProcess.onWritten(0)// start sending
        }
View Full Code Here

            final FutureResponseHandler respHdl = new FutureResponseHandler();
            final BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
            dataSink.setFlushmode(FlushMode.ASYNC);
           
           
            IWriteCompletionHandler completionHandler1 = new IWriteCompletionHandler() {
               
                public void onWritten(int written) throws IOException {
                    try {
                        IHttpResponse response = respHdl.getResponse();
                        Assert.assertEquals(200, response.getStatus());
                       
                        final BodyDataSource dataSource = response.getBody();
                        Assert.assertEquals("test", dataSource.readStringByLength(4));
                       
   
                        IWriteCompletionHandler completionHandler2 = new IWriteCompletionHandler() {
                           
                            public void onWritten(int written) throws IOException {
                                Assert.assertEquals("12345", dataSource.readStringByLength(5));

                                IWriteCompletionHandler completionHandler3 = new IWriteCompletionHandler() {
                                   
                                    public void onWritten(int written) throws IOException {
                                        Assert.assertEquals("789", dataSource.readStringByLength(3));
                                        dataSink.close();
                                    }
View Full Code Here

            final FutureResponseHandler respHdl = new FutureResponseHandler();
            final BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
            dataSink.setFlushmode(FlushMode.ASYNC);
           
           
            IWriteCompletionHandler completionHandler1 = new IWriteCompletionHandler() {
               
                public void onWritten(int written) throws IOException {
                    try {
                        IHttpResponse response = respHdl.getResponse();
                        Assert.assertEquals(200, response.getStatus());
                       
                        final BlockingBodyDataSource dataSource = response.getBlockingBody();
                        Assert.assertEquals("test", dataSource.readStringByLength(4));
                       
   
                        IWriteCompletionHandler completionHandler2 = new IWriteCompletionHandler() {
                           
                            public void onWritten(int written) throws IOException {
                                Assert.assertEquals("12345", dataSource.readStringByLength(5));

                                IWriteCompletionHandler completionHandler3 = new IWriteCompletionHandler() {
                                   
                                    public void onWritten(int written) throws IOException {
                                        Assert.assertEquals("789", dataSource.readStringByLength(3));
                                        dataSink.close();
                                    }
View Full Code Here

           
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final FileChannel fc = raf.getChannel();
           
           
            IWriteCompletionHandler sendFileProcess = new IWriteCompletionHandler() {
               
                private long remaining = file.length();
                private long offset = 0;
                private long length = 0;
               
               
                public void onWritten(int written) throws IOException {
                    write();
                }
               
               
                private void write() throws IOException {
                   
                    // remaining data to write?
                    if (remaining > 0) {
                       
                        // limit the buffer allocation size
                        if (remaining > 4096) {
                            length = 4096;
                        } else {
                            length = remaining;
                        }
                       
                        MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, offset, length);
                        ByteBuffer[] bufs = new ByteBuffer[] { buffer };
                 
                        outChannel.write(bufs, this);
                           
                        offset += length;
                        remaining -= length;

                    // no, closing channel
                    } else {
                        closeFile();
                        outChannel.close();
                    }
                }
               
                public void onException(IOException ioe) {
                    closeFile();
                    outChannel.destroy();
                }
               
               
                private void closeFile() {
                    try {
                        fc.close();
                        raf.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            };
           
            sendFileProcess.onWritten(0)// start sending
        }
View Full Code Here

TOP

Related Classes of org.xsocket.connection.IWriteCompletionHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.