1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.imap.response;
14
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import org.abstracthorizon.mercury.imap.IMAPSession;
18
19
20
21
22
23
24 public abstract class Response {
25
26 public static final int TAGGED_RESPONSE = 1;
27 public static final int UNTAGGED_RESPONSE = 2;
28 public static final int CONTINUATION_RESPONSE = 3;
29
30
31 protected IMAPSession session;
32
33
34 private StringBuffer msg = new StringBuffer();
35
36
37
38
39
40
41
42 public Response(IMAPSession session, int type) {
43 this.session = session;
44 if (type == TAGGED_RESPONSE) {
45 this.msg.append(session.getTag()).append(' ');
46 } else if (type == UNTAGGED_RESPONSE) {
47 this.msg.append("* ");
48 } else if (type == CONTINUATION_RESPONSE) {
49 this.msg.append("+ ");
50 }
51 }
52
53
54
55
56
57
58
59
60 public Response(IMAPSession session, int type, String msg) {
61 this(session, type);
62 this.msg.append(msg);
63 }
64
65
66
67
68
69
70 public Response append(String string) {
71 msg.append(string);
72 return this;
73 }
74
75
76
77
78
79
80 public Response append(StringBuffer sbuf) {
81 msg.append(sbuf);
82 return this;
83 }
84
85
86
87
88
89
90 public Response append(int i) {
91 msg.append(i);
92 return this;
93 }
94
95
96
97
98
99
100 public Response append(long l) {
101 msg.append(l);
102 return this;
103 }
104
105
106
107
108
109
110 public Response append(char c) {
111 msg.append(c);
112 return this;
113 }
114
115
116
117
118
119
120 public Response append(Object object) {
121 msg.append(object);
122 return this;
123 }
124
125
126
127
128
129
130 public void append(byte[] buf) throws IOException {
131 append(buf, 0, buf.length);
132 }
133
134
135
136
137
138
139
140
141 public void append(byte[] buf, int off, int len) throws IOException {
142 OutputStream out = (OutputStream)session.adapt(OutputStream.class);
143 out.write(buf, off, len);
144 out.flush();
145 }
146
147
148
149
150
151 protected void commit() throws IOException {
152 OutputStream out = (OutputStream)session.adapt(OutputStream.class);
153 synchronized (out) {
154 out.write(msg.toString().getBytes());
155 out.write(13);
156 out.write(10);
157 out.flush();
158 }
159 msg.delete(0, msg.length());
160 }
161
162
163
164
165
166 public void submit() throws IOException {
167 if (msg.length() > 0) {
168 OutputStream out = (OutputStream)session.adapt(OutputStream.class);
169 synchronized (out) {
170 out.write(msg.toString().getBytes());
171 out.write(13);
172 out.write(10);
173 out.flush();
174 }
175 msg.delete(0, msg.length());
176 }
177 }
178
179 }