View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.message;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.nio.ByteBuffer;
24  
25  import org.eclipse.jetty.util.Utf8StringBuilder;
26  import org.eclipse.jetty.websocket.common.events.AnnotatedEventDriver;
27  
28  /**
29   * Support class for reading text message data as an Reader.
30   * <p>
31   * Due to the spec, this reader is forced to use the UTF8 charset.
32   */
33  public class MessageReader extends Reader implements MessageAppender
34  {
35      private final AnnotatedEventDriver driver;
36      private final Utf8StringBuilder utf;
37      private int size;
38      private boolean finished;
39      private boolean needsNotification;
40  
41      public MessageReader(AnnotatedEventDriver driver)
42      {
43          this.driver = driver;
44          this.utf = new Utf8StringBuilder();
45          size = 0;
46          finished = false;
47          needsNotification = true;
48      }
49  
50      @Override
51      public void appendMessage(ByteBuffer payload) throws IOException
52      {
53          if (finished)
54          {
55              throw new IOException("Cannot append to finished buffer");
56          }
57  
58          if (payload == null)
59          {
60              // empty payload is valid
61              return;
62          }
63  
64          driver.getPolicy().assertValidMessageSize(size + payload.remaining());
65          size += payload.remaining();
66  
67          synchronized (utf)
68          {
69              utf.append(payload);
70          }
71  
72          if (needsNotification)
73          {
74              needsNotification = true;
75              this.driver.onReader(this);
76          }
77      }
78  
79      @Override
80      public void close() throws IOException
81      {
82          finished = true;
83      }
84  
85      @Override
86      public void messageComplete()
87      {
88          finished = true;
89      }
90  
91      @Override
92      public int read(char[] cbuf, int off, int len) throws IOException
93      {
94          // TODO Auto-generated method stub
95          return 0;
96      }
97  }