View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.nio.reactor;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.UnsupportedEncodingException;
33  import java.nio.ByteBuffer;
34  import java.nio.channels.Channels;
35  import java.nio.channels.ReadableByteChannel;
36  import java.nio.channels.WritableByteChannel;
37  import java.nio.charset.CharacterCodingException;
38  import java.nio.charset.Charset;
39  import java.nio.charset.CharsetDecoder;
40  import java.nio.charset.CharsetEncoder;
41  import java.nio.charset.CodingErrorAction;
42  import java.util.Arrays;
43  import java.util.Collection;
44  
45  import org.apache.http.Consts;
46  import org.apache.http.MessageConstraintException;
47  import org.apache.http.config.MessageConstraints;
48  import org.apache.http.nio.reactor.SessionInputBuffer;
49  import org.apache.http.nio.reactor.SessionOutputBuffer;
50  import org.apache.http.nio.util.ByteBufferAllocator;
51  import org.apache.http.nio.util.DirectByteBufferAllocator;
52  import org.apache.http.nio.util.HeapByteBufferAllocator;
53  import org.apache.http.util.CharArrayBuffer;
54  import org.junit.Assert;
55  import org.junit.Test;
56  import org.junit.runner.RunWith;
57  import org.junit.runners.Parameterized;
58  import org.junit.runners.Parameterized.Parameters;
59  
60  /**
61   * Simple tests for {@link SessionInputBuffer} and {@link SessionOutputBuffer}.
62   */
63  @RunWith(Parameterized.class)
64  public class TestSessionInOutBuffers {
65  
66      private final ByteBufferAllocator allocator;
67  
68      public TestSessionInOutBuffers(final ByteBufferAllocator allocator) {
69          super();
70          this.allocator = allocator;
71      }
72  
73      @Parameters
74      public static Collection<Object[]> getParameters() {
75  
76          return Arrays.asList(
77                  new Object[] { HeapByteBufferAllocator.INSTANCE },
78                  new Object[] { DirectByteBufferAllocator.INSTANCE });
79      }
80  
81      private static WritableByteChannel newChannel(final ByteArrayOutputStream outstream) {
82          return Channels.newChannel(outstream);
83      }
84  
85      private static ReadableByteChannel newChannel(final byte[] bytes) {
86          return Channels.newChannel(new ByteArrayInputStream(bytes));
87      }
88  
89      private static ReadableByteChannel newChannel(final String s, final Charset charset)
90              throws UnsupportedEncodingException {
91          return Channels.newChannel(new ByteArrayInputStream(s.getBytes(charset)));
92      }
93  
94      private static ReadableByteChannel newChannel(final String s)
95              throws UnsupportedEncodingException {
96          return newChannel(s, Consts.ASCII);
97      }
98  
99      @Test
100     public void testReadLineChunks() throws Exception {
101         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, null, this.allocator);
102 
103         ReadableByteChannel channel = newChannel("One\r\nTwo\r\nThree");
104 
105         inbuf.fill(channel);
106 
107         final CharArrayBuffer line = new CharArrayBuffer(64);
108 
109         line.clear();
110         Assert.assertTrue(inbuf.readLine(line, false));
111         Assert.assertEquals("One", line.toString());
112 
113         line.clear();
114         Assert.assertTrue(inbuf.readLine(line, false));
115         Assert.assertEquals("Two", line.toString());
116 
117         line.clear();
118         Assert.assertFalse(inbuf.readLine(line, false));
119 
120         channel = newChannel("\r\nFour");
121         inbuf.fill(channel);
122 
123         line.clear();
124         Assert.assertTrue(inbuf.readLine(line, false));
125         Assert.assertEquals("Three", line.toString());
126 
127         inbuf.fill(channel);
128 
129         line.clear();
130         Assert.assertTrue(inbuf.readLine(line, true));
131         Assert.assertEquals("Four", line.toString());
132 
133         line.clear();
134         Assert.assertFalse(inbuf.readLine(line, true));
135     }
136 
137     @Test
138     public void testLineLimit() throws Exception {
139         final String s = "LoooooooooooooooooooooooooOOOOOOOOOOOOOOOOOOoooooooooooooooooooooong line\r\n";
140         final CharArrayBuffer line = new CharArrayBuffer(64);
141         final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(128, 128,
142                 MessageConstraints.DEFAULT, null, this.allocator);
143         final ReadableByteChannel channel1 = newChannel(s);
144         inbuf1.fill(channel1);
145 
146         Assert.assertTrue(inbuf1.readLine(line, false));
147 
148         line.clear();
149         final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(128, 128,
150                 MessageConstraints.lineLen(10), null, this.allocator);
151         final ReadableByteChannel channel2 = newChannel(s);
152         inbuf2.fill(channel2);
153         try {
154             inbuf2.readLine(line, false);
155             Assert.fail("MessageConstraintException expected");
156         } catch (final MessageConstraintException ex) {
157         }
158     }
159 
160     @Test
161     public void testLineLimitBufferFull() throws Exception {
162         final String s = "LoooooooooooooooooooooooooOOOOOOOOOOOOOOOOOOoooooooooooooooooooooong line\r\n";
163         final CharArrayBuffer line = new CharArrayBuffer(64);
164         final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(32, 32,
165                 MessageConstraints.DEFAULT, null, this.allocator);
166         final ReadableByteChannel channel1 = newChannel(s);
167         inbuf1.fill(channel1);
168 
169         Assert.assertFalse(inbuf1.readLine(line, false));
170 
171         line.clear();
172         final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(32, 32,
173                 MessageConstraints.lineLen(10), null, this.allocator);
174         final ReadableByteChannel channel2 = newChannel(s);
175         inbuf2.fill(channel2);
176         try {
177             inbuf2.readLine(line, false);
178             Assert.fail("MessageConstraintException expected");
179         } catch (final MessageConstraintException ex) {
180         }
181     }
182 
183     @Test
184     public void testWriteLineChunks() throws Exception {
185         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, null, this.allocator);
186         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, null, this.allocator);
187 
188         ReadableByteChannel inChannel = newChannel("One\r\nTwo\r\nThree");
189 
190         inbuf.fill(inChannel);
191 
192         final CharArrayBuffer line = new CharArrayBuffer(64);
193 
194         line.clear();
195         Assert.assertTrue(inbuf.readLine(line, false));
196         Assert.assertEquals("One", line.toString());
197 
198         outbuf.writeLine(line);
199 
200         line.clear();
201         Assert.assertTrue(inbuf.readLine(line, false));
202         Assert.assertEquals("Two", line.toString());
203 
204         outbuf.writeLine(line);
205 
206         line.clear();
207         Assert.assertFalse(inbuf.readLine(line, false));
208 
209         inChannel = newChannel("\r\nFour");
210         inbuf.fill(inChannel);
211 
212         line.clear();
213         Assert.assertTrue(inbuf.readLine(line, false));
214         Assert.assertEquals("Three", line.toString());
215 
216         outbuf.writeLine(line);
217 
218         inbuf.fill(inChannel);
219 
220         line.clear();
221         Assert.assertTrue(inbuf.readLine(line, true));
222         Assert.assertEquals("Four", line.toString());
223 
224         outbuf.writeLine(line);
225 
226         line.clear();
227         Assert.assertFalse(inbuf.readLine(line, true));
228 
229         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
230         final WritableByteChannel outChannel = newChannel(outstream);
231         outbuf.flush(outChannel);
232 
233         final String s = new String(outstream.toByteArray(), "US-ASCII");
234         Assert.assertEquals("One\r\nTwo\r\nThree\r\nFour\r\n", s);
235     }
236 
237     @Test
238     public void testBasicReadWriteLine() throws Exception {
239 
240         final String[] teststrs = new String[5];
241         teststrs[0] = "Hello";
242         teststrs[1] = "This string should be much longer than the size of the line buffer " +
243                 "which is only 16 bytes for this test";
244         final StringBuilder buffer = new StringBuilder();
245         for (int i = 0; i < 15; i++) {
246             buffer.append("123456789 ");
247         }
248         buffer.append("and stuff like that");
249         teststrs[2] = buffer.toString();
250         teststrs[3] = "";
251         teststrs[4] = "And goodbye";
252 
253         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, null, this.allocator);
254         for (final String teststr : teststrs) {
255             outbuf.writeLine(teststr);
256         }
257         //this write operation should have no effect
258         outbuf.writeLine((String)null);
259         outbuf.writeLine((CharArrayBuffer)null);
260 
261         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
262         final WritableByteChannel outChannel = newChannel(outstream);
263         outbuf.flush(outChannel);
264 
265         final ReadableByteChannel channel = newChannel(outstream.toByteArray());
266 
267         final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, null, this.allocator);
268         inbuf.fill(channel);
269 
270         for (final String teststr : teststrs) {
271             Assert.assertEquals(teststr, inbuf.readLine(true));
272         }
273         Assert.assertNull(inbuf.readLine(true));
274         Assert.assertNull(inbuf.readLine(true));
275     }
276 
277     @Test
278     public void testComplexReadWriteLine() throws Exception {
279         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, null, this.allocator);
280         outbuf.write(ByteBuffer.wrap(new byte[] {'a', '\n'}));
281         outbuf.write(ByteBuffer.wrap(new byte[] {'\r', '\n'}));
282         outbuf.write(ByteBuffer.wrap(new byte[] {'\r', '\r', '\n'}));
283         outbuf.write(ByteBuffer.wrap(new byte[] {'\n'}));
284 
285         final StringBuilder buffer = new StringBuilder();
286         for (int i = 0; i < 14; i++) {
287             buffer.append("a");
288         }
289         final String s1 = buffer.toString();
290         buffer.append("\r\n");
291         outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(Consts.ASCII)));
292 
293         buffer.setLength(0);
294         for (int i = 0; i < 15; i++) {
295             buffer.append("a");
296         }
297         final String s2 = buffer.toString();
298         buffer.append("\r\n");
299         outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(Consts.ASCII)));
300 
301         buffer.setLength(0);
302         for (int i = 0; i < 16; i++) {
303             buffer.append("a");
304         }
305         final String s3 = buffer.toString();
306         buffer.append("\r\n");
307         outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(Consts.ASCII)));
308 
309         outbuf.write(ByteBuffer.wrap(new byte[] {'a'}));
310 
311         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
312         final WritableByteChannel outChannel = newChannel(outstream);
313         outbuf.flush(outChannel);
314 
315         final ReadableByteChannel channel = newChannel(outstream.toByteArray());
316 
317         final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, null, this.allocator);
318         inbuf.fill(channel);
319 
320         Assert.assertEquals("a", inbuf.readLine(true));
321         Assert.assertEquals("", inbuf.readLine(true));
322         Assert.assertEquals("\r", inbuf.readLine(true));
323         Assert.assertEquals("", inbuf.readLine(true));
324         Assert.assertEquals(s1, inbuf.readLine(true));
325         Assert.assertEquals(s2, inbuf.readLine(true));
326         Assert.assertEquals(s3, inbuf.readLine(true));
327         Assert.assertEquals("a", inbuf.readLine(true));
328         Assert.assertNull(inbuf.readLine(true));
329         Assert.assertNull(inbuf.readLine(true));
330     }
331 
332     @Test
333     public void testReadOneByte() throws Exception {
334         // make the buffer larger than that of transmitter
335         final byte[] out = new byte[40];
336         for (int i = 0; i < out.length; i++) {
337             out[i] = (byte)('0' + i);
338         }
339         final ReadableByteChannel channel = newChannel(out);
340         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, null, this.allocator);
341         while (inbuf.fill(channel) > 0) {
342         }
343 
344         final byte[] in = new byte[40];
345         for (int i = 0; i < in.length; i++) {
346             in[i] = (byte)inbuf.read();
347         }
348         for (int i = 0; i < out.length; i++) {
349             Assert.assertEquals(out[i], in[i]);
350         }
351     }
352 
353     @Test
354     public void testReadByteBuffer() throws Exception {
355         final byte[] pattern = "0123456789ABCDEF".getBytes(Consts.ASCII);
356         final ReadableByteChannel channel = newChannel(pattern);
357         final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, null, this.allocator);
358         while (inbuf.fill(channel) > 0) {
359         }
360         final ByteBuffer dst = ByteBuffer.allocate(10);
361         Assert.assertEquals(10, inbuf.read(dst));
362         dst.flip();
363         Assert.assertEquals(dst, ByteBuffer.wrap(pattern, 0, 10));
364         dst.clear();
365         Assert.assertEquals(6, inbuf.read(dst));
366         dst.flip();
367         Assert.assertEquals(dst, ByteBuffer.wrap(pattern, 10, 6));
368     }
369 
370     @Test
371     public void testReadByteBufferWithMaxLen() throws Exception {
372         final byte[] pattern = "0123456789ABCDEF".getBytes(Consts.ASCII);
373         final ReadableByteChannel channel = newChannel(pattern);
374         final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, null, this.allocator);
375         while (inbuf.fill(channel) > 0) {
376         }
377         final ByteBuffer dst = ByteBuffer.allocate(16);
378         Assert.assertEquals(10, inbuf.read(dst, 10));
379         dst.flip();
380         Assert.assertEquals(dst, ByteBuffer.wrap(pattern, 0, 10));
381         dst.clear();
382         Assert.assertEquals(3, inbuf.read(dst, 3));
383         dst.flip();
384         Assert.assertEquals(dst, ByteBuffer.wrap(pattern, 10, 3));
385         Assert.assertEquals(3, inbuf.read(dst, 20));
386         dst.flip();
387         Assert.assertEquals(dst, ByteBuffer.wrap(pattern, 13, 3));
388     }
389 
390     @Test
391     public void testReadToChannel() throws Exception {
392         final byte[] pattern = "0123456789ABCDEF".getBytes(Consts.ASCII);
393         final ReadableByteChannel channel = newChannel(pattern);
394         final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, null, this.allocator);
395         while (inbuf.fill(channel) > 0) {
396         }
397 
398         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
399         final WritableByteChannel dst = newChannel(outstream);
400 
401         Assert.assertEquals(16, inbuf.read(dst));
402         Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
403     }
404 
405     @Test
406     public void testReadToChannelWithMaxLen() throws Exception {
407         final byte[] pattern = "0123456789ABCDEF".getBytes(Consts.ASCII);
408         final ReadableByteChannel channel = newChannel(pattern);
409         final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, null, this.allocator);
410         while (inbuf.fill(channel) > 0) {
411         }
412 
413         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
414         final WritableByteChannel dst = newChannel(outstream);
415 
416         Assert.assertEquals(10, inbuf.read(dst, 10));
417         Assert.assertEquals(3, inbuf.read(dst, 3));
418         Assert.assertEquals(3, inbuf.read(dst, 10));
419         Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
420     }
421 
422     @Test
423     public void testWriteByteBuffer() throws Exception {
424         final byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes(Consts.ASCII);
425 
426         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024, null, this.allocator);
427         final ReadableByteChannel src = newChannel(pattern);
428         outbuf.write(src);
429 
430         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
431         final WritableByteChannel channel = newChannel(outstream);
432         while (outbuf.flush(channel) > 0) {
433         }
434         Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
435     }
436 
437     @Test
438     public void testWriteFromChannel() throws Exception {
439         final byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes(Consts.ASCII);
440 
441         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024, null, this.allocator);
442         outbuf.write(ByteBuffer.wrap(pattern, 0, 16));
443         outbuf.write(ByteBuffer.wrap(pattern, 16, 10));
444         outbuf.write(ByteBuffer.wrap(pattern, 26, 6));
445 
446         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
447         final WritableByteChannel channel = newChannel(outstream);
448         while (outbuf.flush(channel) > 0) {
449         }
450         Assert.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outstream.toByteArray()));
451     }
452 
453     static final int SWISS_GERMAN_HELLO [] = {
454         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
455     };
456 
457     static final int RUSSIAN_HELLO [] = {
458         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
459         0x432, 0x435, 0x442
460     };
461 
462     private static String constructString(final int [] unicodeChars) {
463         final StringBuilder buffer = new StringBuilder();
464         if (unicodeChars != null) {
465             for (final int unicodeChar : unicodeChars) {
466                 buffer.append((char)unicodeChar);
467             }
468         }
469         return buffer.toString();
470     }
471 
472     @Test
473     public void testMultibyteCodedReadWriteLine() throws Exception {
474         final String s1 = constructString(SWISS_GERMAN_HELLO);
475         final String s2 = constructString(RUSSIAN_HELLO);
476         final String s3 = "Like hello and stuff";
477 
478         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16,
479                 Consts.UTF_8.newEncoder(), this.allocator);
480 
481         for (int i = 0; i < 10; i++) {
482             outbuf.writeLine(s1);
483             outbuf.writeLine(s2);
484             outbuf.writeLine(s3);
485         }
486 
487         final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
488         final WritableByteChannel outChannel = newChannel(outstream);
489         outbuf.flush(outChannel);
490 
491         final byte[] tmp = outstream.toByteArray();
492 
493         final ReadableByteChannel channel = newChannel(tmp);
494         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16,
495                 Consts.UTF_8.newDecoder(), this.allocator);
496 
497         while (inbuf.fill(channel) > 0) {
498         }
499 
500         for (int i = 0; i < 10; i++) {
501             Assert.assertEquals(s1, inbuf.readLine(true));
502             Assert.assertEquals(s2, inbuf.readLine(true));
503             Assert.assertEquals(s3, inbuf.readLine(true));
504         }
505     }
506 
507     @Test
508     public void testInputMatchesBufferLength() throws Exception {
509         final String s1 = "abcde";
510         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 5, null, this.allocator);
511         outbuf.writeLine(s1);
512     }
513 
514     @Test(expected=CharacterCodingException.class)
515     public void testMalformedInputActionReport() throws Exception {
516         final String s = constructString(SWISS_GERMAN_HELLO);
517         final byte[] tmp = s.getBytes(Consts.ISO_8859_1);
518 
519         final CharsetDecoder decoder = Consts.UTF_8.newDecoder();
520         decoder.onMalformedInput(CodingErrorAction.REPORT);
521         decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
522         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, decoder, this.allocator);
523         final ReadableByteChannel channel = newChannel(tmp);
524         while (inbuf.fill(channel) > 0) {
525         }
526         inbuf.readLine(true);
527     }
528 
529     @Test
530     public void testMalformedInputActionIgnore() throws Exception {
531         final String s = constructString(SWISS_GERMAN_HELLO);
532         final byte[] tmp = s.getBytes(Consts.ISO_8859_1);
533 
534         final CharsetDecoder decoder = Consts.UTF_8.newDecoder();
535         decoder.onMalformedInput(CodingErrorAction.IGNORE);
536         decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
537         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, decoder, this.allocator);
538         final ReadableByteChannel channel = newChannel(tmp);
539         while (inbuf.fill(channel) > 0) {
540         }
541         final String result = inbuf.readLine(true);
542         Assert.assertEquals("Grezi_zm", result);
543     }
544 
545     @Test
546     public void testMalformedInputActionReplace() throws Exception {
547         final String s = constructString(SWISS_GERMAN_HELLO);
548         final byte[] tmp = s.getBytes(Consts.ISO_8859_1);
549 
550         final CharsetDecoder decoder = Consts.UTF_8.newDecoder();
551         decoder.onMalformedInput(CodingErrorAction.REPLACE);
552         decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
553         final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, decoder, this.allocator);
554         final ReadableByteChannel channel = newChannel(tmp);
555         while (inbuf.fill(channel) > 0) {
556         }
557         final String result = inbuf.readLine(true);
558         Assert.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", result);
559     }
560 
561     @Test(expected=CharacterCodingException.class)
562     public void testUnmappableInputActionReport() throws Exception {
563         final String s = "This text contains a circumflex \u0302!!!";
564         final CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
565         encoder.onMalformedInput(CodingErrorAction.IGNORE);
566         encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
567         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder, this.allocator);
568         outbuf.writeLine(s);
569     }
570 
571     @Test
572     public void testUnmappableInputActionIgnore() throws Exception {
573         final String s = "This text contains a circumflex \u0302!!!";
574         final CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
575         encoder.onMalformedInput(CodingErrorAction.IGNORE);
576         encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
577         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder, this.allocator);
578         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
579         final WritableByteChannel channel = newChannel(baos);
580         outbuf.writeLine(s);
581         outbuf.flush(channel);
582 
583         final String result = new String(baos.toByteArray(), "US-ASCII");
584         Assert.assertEquals("This text contains a circumflex !!!\r\n", result);
585     }
586 
587     @Test
588     public void testUnmappableInputActionReplace() throws Exception {
589         final String s = "This text contains a circumflex \u0302 !!!";
590         final CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
591         encoder.onMalformedInput(CodingErrorAction.IGNORE);
592         encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
593         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder, this.allocator);
594         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
595         final WritableByteChannel channel = newChannel(baos);
596         outbuf.writeLine(s);
597         outbuf.flush(channel);
598 
599         final String result = new String(baos.toByteArray(), "US-ASCII");
600         Assert.assertEquals("This text contains a circumflex ? !!!\r\n", result);
601     }
602 
603 }