91 // All synchronization and state/argument checking is done in these public
92 // methods; the concrete stream-encoder subclasses defined below need not
93 // do any such checking.
94
95 public String getEncoding() {
96 if (isOpen())
97 return encodingName();
98 return null;
99 }
100
101 public void flushBuffer() throws IOException {
102 synchronized (lock) {
103 if (isOpen())
104 implFlushBuffer();
105 else
106 throw new IOException("Stream closed");
107 }
108 }
109
110 public void write(int c) throws IOException {
111 char cbuf[] = new char[1];
112 cbuf[0] = (char) c;
113 write(cbuf, 0, 1);
114 }
115
116 public void write(char cbuf[], int off, int len) throws IOException {
117 synchronized (lock) {
118 ensureOpen();
119 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
120 ((off + len) > cbuf.length) || ((off + len) < 0)) {
121 throw new IndexOutOfBoundsException();
122 } else if (len == 0) {
123 return;
124 }
125 implWrite(cbuf, off, len);
126 }
127 }
128
129 public void write(String str, int off, int len) throws IOException {
130 /* Check the len before creating a char buffer */
131 if (len < 0)
132 throw new IndexOutOfBoundsException();
133 char cbuf[] = new char[len];
134 str.getChars(off, off + len, cbuf, 0);
135 write(cbuf, 0, len);
136 }
137
138 public void write(CharBuffer cb) throws IOException {
139 int position = cb.position();
140 try {
141 synchronized (lock) {
142 ensureOpen();
143 implWrite(cb);
144 }
145 } finally {
146 cb.position(position);
147 }
148 }
149
150 public void flush() throws IOException {
151 synchronized (lock) {
152 ensureOpen();
153 implFlush();
257 leftoverChar = lcb.get();
258 if (cb != null && cb.hasRemaining()) {
259 lcb.clear();
260 lcb.put(leftoverChar).put(cb.get()).flip();
261 continue;
262 }
263 return;
264 }
265 break;
266 }
267 if (cr.isOverflow()) {
268 assert bb.position() > 0;
269 writeBytes();
270 continue;
271 }
272 cr.throwException();
273 }
274 haveLeftoverChar = false;
275 }
276
277 void implWrite(char cbuf[], int off, int len)
278 throws IOException
279 {
280 CharBuffer cb = CharBuffer.wrap(cbuf, off, len);
281 implWrite(cb);
282 }
283
284 void implWrite(CharBuffer cb)
285 throws IOException
286 {
287 if (haveLeftoverChar) {
288 flushLeftoverChar(cb, false);
289 }
290
291 while (cb.hasRemaining()) {
292 CoderResult cr = encoder.encode(cb, bb, false);
293 if (cr.isUnderflow()) {
294 assert (cb.remaining() <= 1) : cb.remaining();
295 if (cb.remaining() == 1) {
296 haveLeftoverChar = true;
297 leftoverChar = cb.get();
|
91 // All synchronization and state/argument checking is done in these public
92 // methods; the concrete stream-encoder subclasses defined below need not
93 // do any such checking.
94
95 public String getEncoding() {
96 if (isOpen())
97 return encodingName();
98 return null;
99 }
100
101 public void flushBuffer() throws IOException {
102 synchronized (lock) {
103 if (isOpen())
104 implFlushBuffer();
105 else
106 throw new IOException("Stream closed");
107 }
108 }
109
110 public void write(int c) throws IOException {
111 char[] cbuf = new char[1];
112 cbuf[0] = (char) c;
113 write(cbuf, 0, 1);
114 }
115
116 public void write(char[] cbuf, int off, int len) throws IOException {
117 synchronized (lock) {
118 ensureOpen();
119 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
120 ((off + len) > cbuf.length) || ((off + len) < 0)) {
121 throw new IndexOutOfBoundsException();
122 } else if (len == 0) {
123 return;
124 }
125 implWrite(cbuf, off, len);
126 }
127 }
128
129 public void write(String str, int off, int len) throws IOException {
130 /* Check the len before creating a char buffer */
131 if (len < 0)
132 throw new IndexOutOfBoundsException();
133 char[] cbuf = new char[len];
134 str.getChars(off, off + len, cbuf, 0);
135 write(cbuf, 0, len);
136 }
137
138 public void write(CharBuffer cb) throws IOException {
139 int position = cb.position();
140 try {
141 synchronized (lock) {
142 ensureOpen();
143 implWrite(cb);
144 }
145 } finally {
146 cb.position(position);
147 }
148 }
149
150 public void flush() throws IOException {
151 synchronized (lock) {
152 ensureOpen();
153 implFlush();
257 leftoverChar = lcb.get();
258 if (cb != null && cb.hasRemaining()) {
259 lcb.clear();
260 lcb.put(leftoverChar).put(cb.get()).flip();
261 continue;
262 }
263 return;
264 }
265 break;
266 }
267 if (cr.isOverflow()) {
268 assert bb.position() > 0;
269 writeBytes();
270 continue;
271 }
272 cr.throwException();
273 }
274 haveLeftoverChar = false;
275 }
276
277 void implWrite(char[] cbuf, int off, int len)
278 throws IOException
279 {
280 CharBuffer cb = CharBuffer.wrap(cbuf, off, len);
281 implWrite(cb);
282 }
283
284 void implWrite(CharBuffer cb)
285 throws IOException
286 {
287 if (haveLeftoverChar) {
288 flushLeftoverChar(cb, false);
289 }
290
291 while (cb.hasRemaining()) {
292 CoderResult cr = encoder.encode(cb, bb, false);
293 if (cr.isUnderflow()) {
294 assert (cb.remaining() <= 1) : cb.remaining();
295 if (cb.remaining() == 1) {
296 haveLeftoverChar = true;
297 leftoverChar = cb.get();
|