1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
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
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
95 return 0;
96 }
97 }