Guest User

Untitled

a guest
Mar 3rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1.     /**
  2.      * Cumulates content of <tt>in</tt> into internal buffer and forwards
  3.      * decoding request to
  4.      * {@link #doDecode(IoSession, IoBuffer, ProtocolDecoderOutput)}.
  5.      * <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt>
  6.      * and the cumulative buffer is compacted after decoding ends.
  7.      *
  8.      * @throws IllegalStateException
  9.      *             if your <tt>doDecode()</tt> returned <tt>true</tt> not
  10.      *             consuming the cumulative buffer.
  11.      */
  12.     public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
  13. //        if (!session.getTransportMetadata().hasFragmentation()) {
  14.             while (in.hasRemaining()) {
  15.                 if (!doDecode(session, in, out)) {
  16.                     break;
  17.                 }
  18.             }
  19.  
  20. //            return;
  21. //        }
  22.  
  23.         boolean usingSessionBuffer = true;
  24.         IoBuffer buf = (IoBuffer) session.getAttribute(BUFFER);
  25.         // If we have a session buffer, append data to that; otherwise
  26.         // use the buffer read from the network directly.
  27.         if (buf != null) {
  28.             boolean appended = false;
  29.             // Make sure that the buffer is auto-expanded.
  30.             if (buf.isAutoExpand()) {
  31.                 try {
  32.                     buf.put(in);
  33.                     appended = true;
  34.                 } catch (IllegalStateException e) {
  35.                     // A user called derivation method (e.g. slice()),
  36.                     // which disables auto-expansion of the parent buffer.
  37.                 } catch (IndexOutOfBoundsException e) {
  38.                     // A user disabled auto-expansion.
  39.                 }
  40.             }
  41.  
  42.             if (appended) {
  43.                 buf.flip();
  44.             } else {
  45.                 // Reallocate the buffer if append operation failed due to
  46.                 // derivation or disabled auto-expansion.
  47.                 buf.flip();
  48.                 IoBuffer newBuf = IoBuffer.allocate(buf.remaining() + in.remaining()).setAutoExpand(true);
  49.                 newBuf.order(buf.order());
  50.                 newBuf.put(buf);
  51.                 newBuf.put(in);
  52.                 newBuf.flip();
  53.                 buf = newBuf;
  54.  
  55.                 // Update the session attribute.
  56.                 session.setAttribute(BUFFER, buf);
  57.             }
  58.         } else {
  59.             buf = in;
  60.             usingSessionBuffer = false;
  61.         }
  62.  
  63.         for (;;) {
  64.             int oldPos = buf.position();
  65.             boolean decoded = doDecode(session, buf, out);
  66.             if (decoded) {
  67.                 if (buf.position() == oldPos) {
  68.                     throw new IllegalStateException("doDecode() can't return true when buffer is not consumed.");
  69.                 }
  70.  
  71.                 if (!buf.hasRemaining()) {
  72.                     break;
  73.                 }
  74.             } else {
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         // if there is any data left that cannot be decoded, we store
  80.         // it in a buffer in the session and next time this decoder is
  81.         // invoked the session buffer gets appended to
  82.         if (buf.hasRemaining()) {
  83.             if (usingSessionBuffer && buf.isAutoExpand()) {
  84.                 buf.compact();
  85.             } else {
  86.                 storeRemainingInSession(buf, session);
  87.             }
  88.         } else {
  89.             if (usingSessionBuffer) {
  90.                 removeSessionBuffer(session);
  91.             }
  92.         }
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment