View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.io;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.util.log.Logger;
25  import org.eclipse.jetty.websocket.api.extensions.Frame;
26  
27  public class DataFrameBytes extends FrameBytes
28  {
29      private static final Logger LOG = Log.getLogger(DataFrameBytes.class);
30      private ByteBuffer buffer;
31  
32      public DataFrameBytes(AbstractWebSocketConnection connection, Frame frame)
33      {
34          super(connection,frame);
35      }
36  
37      @Override
38      public void completed(Void result)
39      {
40          if (LOG.isDebugEnabled())
41          {
42              LOG.debug("completed({}) - frame.remaining() = {}",result,frame.remaining());
43          }
44  
45          connection.getBufferPool().release(buffer);
46  
47          if (frame.remaining() > 0)
48          {
49              LOG.debug("More to send");
50              // We have written a partial frame per windowing size.
51              // We need to keep the correct ordering of frames, to avoid that another
52              // Data frame for the same stream is written before this one is finished.
53              connection.getQueue().prepend(this);
54              connection.complete(this);
55          }
56          else
57          {
58              LOG.debug("Send complete");
59              super.completed(result);
60          }
61          connection.flush();
62      }
63  
64      @Override
65      public ByteBuffer getByteBuffer()
66      {
67          try
68          {
69              int windowSize = connection.getInputBufferSize();
70              buffer = connection.getGenerator().generate(windowSize,frame);
71              return buffer;
72          }
73          catch (Throwable x)
74          {
75              failed(null,x);
76              return null;
77          }
78      }
79  }