1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.io;
19
20import java.io.BufferedOutputStream;
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.IOException;
24import java.io.OutputStream;
25
26public class BufferedOutputStreamTest extends junit.framework.TestCase {
27
28    private java.io.OutputStream os;
29
30    java.io.ByteArrayOutputStream baos;
31
32    java.io.ByteArrayInputStream bais;
33
34    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
35
36    /**
37     * java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream)
38     */
39    public void test_ConstructorLjava_io_OutputStream() throws IOException {
40        baos = new java.io.ByteArrayOutputStream();
41        os = new java.io.BufferedOutputStream(baos);
42        os.write(fileString.getBytes(), 0, 500);
43    }
44
45    /**
46     * java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream,
47     *int)
48     */
49    public void test_ConstructorLjava_io_OutputStreamI() throws IOException {
50        baos = new java.io.ByteArrayOutputStream();
51        os = new java.io.BufferedOutputStream(baos, 1024);
52        os.write(fileString.getBytes(), 0, 500);
53    }
54
55    public void test_flush_Constructor_NullStream() throws IOException {
56        BufferedOutputStream buffos = new java.io.BufferedOutputStream(null);
57        try {
58            buffos.flush();
59            fail("should throw NullPointerException");
60        } catch (NullPointerException e) {
61            // Expected
62        }
63        try {
64            buffos.close();
65            fail("should throw NullPointerException");
66        } catch (NullPointerException e) {
67            // Expected
68        }
69
70        buffos = new java.io.BufferedOutputStream(null, 10);
71        try {
72            buffos.flush();
73            fail("should throw NullPointerException");
74        } catch (NullPointerException e) {
75            // Expected
76        }
77        try {
78            buffos.close();
79            fail("should throw NullPointerException");
80        } catch (NullPointerException e) {
81            // Expected
82        }
83
84        try {
85            new java.io.BufferedOutputStream(null, 0);
86            fail("should throw IllegalArgumentException");
87        } catch (IllegalArgumentException e) {
88            // Expected
89        }
90        try {
91            new java.io.BufferedOutputStream(null, -1);
92            fail("should throw IllegalArgumentException");
93        } catch (IllegalArgumentException e) {
94            // Expected
95        }
96    }
97
98    /**
99     * java.io.BufferedOutputStream#flush()
100     */
101    public void test_flush() throws IOException {
102        baos = new ByteArrayOutputStream();
103        os = new java.io.BufferedOutputStream(baos, 600);
104        os.write(fileString.getBytes(), 0, 500);
105        os.flush();
106        assertEquals("Bytes not written after flush", 500,
107                ((ByteArrayOutputStream) baos).size());
108    }
109
110    private static class MockOutputStream extends OutputStream {
111        byte[] written;
112        int count;
113
114        public MockOutputStream(int size) {
115            written = new byte[size];
116            count = 0;
117        }
118
119        public void write(int b) {
120            written[count++] = (byte) b;
121        }
122
123        public String getWritten() {
124            return new String(written, 0, count);
125        }
126    }
127
128    /**
129     * java.io.BufferedOutputStream#write(byte[], int, int)
130     */
131    public void test_write$BII() throws IOException {
132        os = new BufferedOutputStream(baos = new ByteArrayOutputStream(), 512);
133        os.write(fileString.getBytes(), 0, 500);
134        bais = new ByteArrayInputStream(baos.toByteArray());
135        assertEquals("Bytes written, not buffered", 0, bais.available());
136        os.flush();
137        bais = new ByteArrayInputStream(baos.toByteArray());
138        assertEquals("Bytes not written after flush", 500, bais.available());
139        os.write(fileString.getBytes(), 500, 513);
140        bais = new ByteArrayInputStream(baos.toByteArray());
141        assertTrue("Bytes not written when buffer full",
142                bais.available() >= 1000);
143        byte[] wbytes = new byte[1013];
144        bais.read(wbytes, 0, 1013);
145        assertEquals("Incorrect bytes written", new String(wbytes, 0,
146                wbytes.length), fileString.substring(0, 1013));
147
148        // regression test for HARMONY-4177
149        MockOutputStream mos = new MockOutputStream(5);
150        BufferedOutputStream bos = new BufferedOutputStream(mos, 3);
151        bos.write("a".getBytes());
152        bos.write("bcde".getBytes());
153        assertEquals("Large data should be written directly", "abcde", mos
154                .getWritten());
155        mos = new MockOutputStream(4);
156        bos = new BufferedOutputStream(mos, 3);
157        bos.write("ab".getBytes());
158        bos.write("cd".getBytes());
159        assertEquals("Should flush before write", "ab", mos.getWritten());
160    }
161
162    /**
163     * java.io.BufferedOutputStream#write(byte[], int, int)
164     */
165    public void test_write_$BII_Exception() throws IOException {
166        OutputStream bos = new BufferedOutputStream(new ByteArrayOutputStream());
167        byte[] nullByteArray = null;
168        byte[] byteArray = new byte[10];
169
170        try {
171            bos.write(nullByteArray, -1, -1);
172            fail("should throw NullPointerException");
173        } catch (NullPointerException e) {
174            // expected
175        }
176
177        try {
178            bos.write(nullByteArray, -1, 0);
179            fail("should throw NullPointerException");
180        } catch (NullPointerException e) {
181            // expected
182        }
183
184        try {
185            bos.write(nullByteArray, -1, 1);
186            fail("should throw NullPointerException");
187        } catch (NullPointerException e) {
188            // expected
189        }
190
191        try {
192            bos.write(nullByteArray, 0, -1);
193            fail("should throw NullPointerException");
194        } catch (NullPointerException e) {
195            // expected
196        }
197
198        try {
199            bos.write(nullByteArray, 0, 0);
200            fail("should throw NullPointerException");
201        } catch (NullPointerException e) {
202            // expected
203        }
204
205        try {
206            bos.write(nullByteArray, 0, 1);
207            fail("should throw NullPointerException");
208        } catch (NullPointerException e) {
209            // expected
210        }
211
212        try {
213            bos.write(nullByteArray, 1, -1);
214            fail("should throw NullPointerException");
215        } catch (NullPointerException e) {
216            // expected
217        }
218
219        try {
220            bos.write(nullByteArray, 1, 0);
221            fail("should throw NullPointerException");
222        } catch (NullPointerException e) {
223            // expected
224        }
225
226        try {
227            bos.write(nullByteArray, 1, 1);
228            fail("should throw NullPointerException");
229        } catch (NullPointerException e) {
230            // expected
231        }
232
233        try {
234            bos.write(byteArray, -1, -1);
235            fail("should throw ArrayIndexOutOfBoundsException");
236        } catch (ArrayIndexOutOfBoundsException e) {
237            // expected
238        }
239
240        try {
241            bos.write(byteArray, -1, 0);
242            fail("should throw ArrayIndexOutOfBoundsException");
243        } catch (ArrayIndexOutOfBoundsException e) {
244            // expected
245        }
246
247        try {
248            bos.write(byteArray, -1, 1);
249            fail("should throw ArrayIndexOutOfBoundsException");
250        } catch (ArrayIndexOutOfBoundsException e) {
251            // expected
252        }
253
254        try {
255            bos.write(byteArray, 0, -1);
256            fail("should throw ArrayIndexOutOfBoundsException");
257        } catch (ArrayIndexOutOfBoundsException e) {
258            // expected
259        }
260
261        try {
262            bos.write(byteArray, 0, byteArray.length + 1);
263            fail("should throw ArrayIndexOutOfBoundsException");
264        } catch (ArrayIndexOutOfBoundsException e) {
265            // expected
266        }
267
268        try {
269            bos.write(byteArray, 1, byteArray.length);
270            fail("should throw ArrayIndexOutOfBoundsException");
271        } catch (ArrayIndexOutOfBoundsException e) {
272            // expected
273        }
274
275        try {
276            bos.write(byteArray, -1, byteArray.length);
277            fail("should throw ArrayIndexOutOfBoundsException");
278        } catch (ArrayIndexOutOfBoundsException e) {
279            // expected
280        }
281
282        try {
283            bos.write(byteArray, byteArray.length, -1);
284            fail("should throw ArrayIndexOutOfBoundsException");
285        } catch (ArrayIndexOutOfBoundsException e) {
286            // expected
287        }
288        bos.write(byteArray, byteArray.length, 0);
289        try {
290            bos.write(byteArray, byteArray.length, 1);
291            fail("should throw ArrayIndexOutOfBoundsException");
292        } catch (ArrayIndexOutOfBoundsException e) {
293            // expected
294        }
295
296        bos.write(byteArray, 0, 0);
297        bos.write(byteArray, 0, 1);
298        bos.write(byteArray, 1, byteArray.length - 1);
299        bos.write(byteArray, 0, byteArray.length);
300
301        try {
302            bos.write(byteArray, 1, -1);
303            fail("should throw ArrayIndexOutOfBoundsException");
304        } catch (ArrayIndexOutOfBoundsException e) {
305            // expected
306        }
307
308        bos.write(byteArray, 1, 0);
309        bos.write(byteArray, 1, 1);
310
311        bos.write(byteArray, byteArray.length, 0);
312
313        try {
314            bos.write(byteArray, byteArray.length + 1, 0);
315            fail("should throw ArrayIndexOutOfBoundsException");
316        } catch (ArrayIndexOutOfBoundsException e) {
317            // expected
318        }
319
320        try {
321            bos.write(byteArray, byteArray.length + 1, 1);
322            fail("should throw ArrayIndexOutOfBoundsException");
323        } catch (ArrayIndexOutOfBoundsException e) {
324            // expected
325        }
326
327        bos.close();
328
329        try {
330            bos.write(byteArray, -1, -1);
331            fail();
332        } catch (IOException expected) {
333        } catch (IndexOutOfBoundsException expected) {
334        }
335
336        try {
337            bos.write(null, -1, -1);
338            fail();
339        } catch (IOException expected) {
340        } catch (NullPointerException expected) {
341        }
342
343        try {
344            bos.write(null, 0, 1);
345            fail();
346        } catch (IOException expected) {
347        } catch (NullPointerException expected) {
348        }
349    }
350
351    /**
352     * java.io.BufferedOutputStream#write(byte[], int, int)
353     */
354    public void test_write_$BII_NullStream_NullArray() throws IOException {
355        OutputStream bos = new BufferedOutputStream(null);
356        byte[] nullByteArray = null;
357
358        try {
359            bos.write(nullByteArray, -1, -1);
360            fail("should throw NullPointerException");
361        } catch (NullPointerException e) {
362            // expected
363        }
364
365        try {
366            bos.write(nullByteArray, 0, -1);
367            fail("should throw NullPointerException");
368        } catch (NullPointerException e) {
369            // expected
370        }
371
372        try {
373            bos.write(nullByteArray, 1, -1);
374            fail("should throw NullPointerException");
375        } catch (NullPointerException e) {
376            // expected
377        }
378
379        try {
380            bos.write(nullByteArray, -1, 0);
381            fail("should throw NullPointerException");
382        } catch (NullPointerException e) {
383            // expected
384        }
385
386        try {
387            bos.write(nullByteArray, 0, 0);
388            fail("should throw NullPointerException");
389        } catch (NullPointerException e) {
390            // expected
391        }
392
393        try {
394            bos.write(nullByteArray, 1, 0);
395            fail("should throw NullPointerException");
396        } catch (NullPointerException e) {
397            // expected
398        }
399
400        try {
401            bos.write(nullByteArray, -1, 1);
402            fail("should throw NullPointerException");
403        } catch (NullPointerException e) {
404            // expected
405        }
406
407        try {
408            bos.write(nullByteArray, 0, 1);
409            fail("should throw NullPointerException");
410        } catch (NullPointerException e) {
411            // expected
412        }
413
414        try {
415            bos.write(nullByteArray, 1, 1);
416            fail("should throw NullPointerException");
417        } catch (NullPointerException e) {
418            // expected
419        }
420    }
421
422    /**
423     * java.io.BufferedOutputStream#write(byte[], int, int)
424     */
425    public void test_write_$BII_NullStream_NullArray_Size() throws IOException {
426        OutputStream bos = new BufferedOutputStream(null, 1);
427        byte[] nullByteArray = null;
428
429        try {
430            bos.write(nullByteArray, -1, -1);
431            fail("should throw NullPointerException");
432        } catch (NullPointerException e) {
433            // expected
434        }
435
436        try {
437            bos.write(nullByteArray, 0, -1);
438            fail("should throw NullPointerException");
439        } catch (NullPointerException e) {
440            // expected
441        }
442
443        try {
444            bos.write(nullByteArray, 1, -1);
445            fail("should throw NullPointerException");
446        } catch (NullPointerException e) {
447            // expected
448        }
449
450        try {
451            bos.write(nullByteArray, -1, 0);
452            fail("should throw NullPointerException");
453        } catch (NullPointerException e) {
454            // expected
455        }
456
457        try {
458            bos.write(nullByteArray, 0, 0);
459            fail("should throw NullPointerException");
460        } catch (NullPointerException e) {
461            // expected
462        }
463
464        try {
465            bos.write(nullByteArray, 1, 0);
466            fail("should throw NullPointerException");
467        } catch (NullPointerException e) {
468            // expected
469        }
470
471        try {
472            bos.write(nullByteArray, -1, 1);
473            fail("should throw NullPointerException");
474        } catch (NullPointerException e) {
475            // expected
476        }
477
478        try {
479            bos.write(nullByteArray, 0, 1);
480            fail("should throw NullPointerException");
481        } catch (NullPointerException e) {
482            // expected
483        }
484
485        try {
486            bos.write(nullByteArray, 1, 1);
487            fail("should throw NullPointerException");
488        } catch (NullPointerException e) {
489            // expected
490        }
491    }
492
493    /**
494     * java.io.BufferedOutputStream#write(byte[], int, int)
495     */
496    public void test_write_$BII_NullStream() throws IOException {
497        BufferedOutputStream bos = new BufferedOutputStream(null);
498        byte[] byteArray = new byte[10];
499
500        try {
501            bos.write(byteArray, -1, -1);
502            fail("should throw ArrayIndexOutOfBoundsException");
503        } catch (ArrayIndexOutOfBoundsException e) {
504            // expected
505        }
506
507        try {
508            bos.write(byteArray, 0, -1);
509            fail("should throw ArrayIndexOutOfBoundsException");
510        } catch (ArrayIndexOutOfBoundsException e) {
511            // expected
512        }
513
514        try {
515            bos.write(byteArray, 1, -1);
516            fail("should throw ArrayIndexOutOfBoundsException");
517        } catch (ArrayIndexOutOfBoundsException e) {
518            // expected
519        }
520
521        try {
522            bos.write(byteArray, -1, 0);
523            fail("should throw ArrayIndexOutOfBoundsException");
524        } catch (ArrayIndexOutOfBoundsException e) {
525            // expected
526        }
527
528        bos.write(byteArray, 0, 0);
529
530        bos.write(byteArray, 1, 0);
531
532        bos.write(byteArray, byteArray.length, 0);
533
534        try {
535            bos.write(byteArray, byteArray.length + 1, 0);
536            fail("should throw ArrayIndexOutOfBoundsException");
537        } catch (ArrayIndexOutOfBoundsException e) {
538            // expected
539        }
540
541        try {
542            bos.write(byteArray, -1, 1);
543            fail("should throw ArrayIndexOutOfBoundsException");
544        } catch (ArrayIndexOutOfBoundsException e) {
545            // expected
546        }
547
548        bos.write(byteArray, 0, 1);
549        bos.write(byteArray, 1, 1);
550
551        bos.write(byteArray, 0, byteArray.length);
552
553        try {
554            bos.write(byteArray, byteArray.length + 1, 1);
555            fail("should throw ArrayIndexOutOfBoundsException");
556        } catch (ArrayIndexOutOfBoundsException e) {
557            // expected
558        }
559    }
560
561    /**
562     * java.io.BufferedOutputStream#write(byte[], int, int)
563     */
564    public void test_write_$BII_NullStream_Size() throws IOException {
565        BufferedOutputStream bos = new BufferedOutputStream(null, 1);
566        byte[] byteArray = new byte[10];
567
568        try {
569            bos.write(byteArray, -1, -1);
570            fail("should throw ArrayIndexOutOfBoundsException");
571        } catch (ArrayIndexOutOfBoundsException e) {
572            // expected
573        }
574
575        try {
576            bos.write(byteArray, 0, -1);
577            fail("should throw ArrayIndexOutOfBoundsException");
578        } catch (ArrayIndexOutOfBoundsException e) {
579            // expected
580        }
581
582        try {
583            bos.write(byteArray, 1, -1);
584            fail("should throw ArrayIndexOutOfBoundsException");
585        } catch (ArrayIndexOutOfBoundsException e) {
586            // expected
587        }
588
589        try {
590            bos.write(byteArray, -1, 0);
591            fail("should throw ArrayIndexOutOfBoundsException");
592        } catch (ArrayIndexOutOfBoundsException e) {
593            // expected
594        }
595
596        bos.write(byteArray, 0, 0);
597
598        bos.write(byteArray, 1, 0);
599
600        bos.write(byteArray, byteArray.length, 0);
601
602        try {
603            bos.write(byteArray, byteArray.length + 1, 0);
604            fail("should throw ArrayIndexOutOfBoundsException");
605        } catch (ArrayIndexOutOfBoundsException e) {
606            // expected
607        }
608
609        try {
610            bos.write(byteArray, -1, 1);
611            fail("should throw NullPointerException");
612        } catch (NullPointerException e) {
613            // expected
614        }
615
616        try {
617            bos.write(byteArray, 0, 1);
618            fail("should throw NullPointerException");
619        } catch (NullPointerException e) {
620            // expected
621        }
622
623        try {
624            bos.write(byteArray, 0, byteArray.length);
625            fail("should throw NullPointerException");
626        } catch (NullPointerException e) {
627            // expected
628        }
629
630        try {
631            bos.write(byteArray, 1, 1);
632            fail("should throw NullPointerException");
633        } catch (NullPointerException e) {
634            // expected
635        }
636
637        try {
638            bos.write(byteArray, byteArray.length + 1, 1);
639            fail("should throw NullPointerException");
640        } catch (NullPointerException e) {
641            // expected
642        }
643    }
644
645    /**
646     * java.io.BufferedOutputStream#write(int)
647     */
648    public void test_writeI() throws IOException {
649        baos = new java.io.ByteArrayOutputStream();
650        os = new java.io.BufferedOutputStream(baos);
651        os.write('t');
652        bais = new java.io.ByteArrayInputStream(baos.toByteArray());
653        assertEquals("Byte written, not buffered", 0, bais.available());
654        os.flush();
655        bais = new java.io.ByteArrayInputStream(baos.toByteArray());
656        assertEquals("Byte not written after flush", 1, bais.available());
657        byte[] wbytes = new byte[1];
658        bais.read(wbytes, 0, 1);
659        assertEquals("Incorrect byte written", 't', wbytes[0]);
660    }
661
662    public void test_write_Close() throws IOException {
663        BufferedOutputStream buffos = new BufferedOutputStream(
664                new ByteArrayOutputStream());
665        buffos.write(new byte[0]);
666        try {
667            buffos.write(null);
668            fail("should throw NullPointerException");
669        } catch (NullPointerException e) {
670            // Expected
671        }
672        byte[] buffer = "1234567890".getBytes();
673
674        buffos.write(Integer.MIN_VALUE);
675        buffos.write(Integer.MAX_VALUE);
676        buffos.write(buffer, 0, 10);
677        buffos.flush();
678
679        buffos.close();
680    }
681
682    public void test_write_Scenario1() throws IOException {
683        ByteArrayOutputStream byteArrayos = new ByteArrayOutputStream();
684        ByteArrayInputStream byteArrayis = null;
685        byte[] buffer = "1234567890".getBytes("UTF-8");
686
687        BufferedOutputStream buffos = new BufferedOutputStream(byteArrayos, 10);
688        buffos.write(buffer, 0, 10);
689        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
690        assertEquals("Bytes written, not buffered", 10, byteArrayis.available());
691        buffos.flush();
692        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
693        assertEquals("Bytes not written after flush", 10, byteArrayis
694                .available());
695        for (int i = 0; i < 10; i++) {
696            assertEquals(buffer[i], byteArrayis.read());
697        }
698
699        buffos.write(buffer, 0, 10);
700        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
701        assertEquals("Bytes written, not buffered", 20, byteArrayis.available());
702        buffos.flush();
703        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
704        assertEquals("Bytes not written after flush", 20, byteArrayis
705                .available());
706        for (int i = 0; i < 10; i++) {
707            assertEquals(buffer[i], byteArrayis.read());
708        }
709        for (int i = 0; i < 10; i++) {
710            assertEquals(buffer[i], byteArrayis.read());
711        }
712
713        buffos.write(buffer, 0, 10);
714        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
715        assertEquals("Bytes written, not buffered", 30, byteArrayis.available());
716        buffos.flush();
717        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
718        assertEquals("Bytes not written after flush", 30, byteArrayis
719                .available());
720        for (int i = 0; i < 10; i++) {
721            assertEquals(buffer[i], byteArrayis.read());
722        }
723        for (int i = 0; i < 10; i++) {
724            assertEquals(buffer[i], byteArrayis.read());
725        }
726        for (int i = 0; i < 10; i++) {
727            assertEquals(buffer[i], byteArrayis.read());
728        }
729    }
730
731    public void test_write_Scenario2() throws IOException {
732        ByteArrayOutputStream byteArrayos = new ByteArrayOutputStream();
733        ByteArrayInputStream byteArrayis = null;
734        byte[] buffer = "1234567890".getBytes("UTF-8");
735
736        BufferedOutputStream buffos = new BufferedOutputStream(byteArrayos, 20);
737        buffos.write(buffer, 0, 10);
738        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
739        assertEquals("Bytes written, not buffered", 0, byteArrayis.available());
740        buffos.flush();
741        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
742        assertEquals("Bytes not written after flush", 10, byteArrayis
743                .available());
744        for (int i = 0; i < 10; i++) {
745            assertEquals(buffer[i], byteArrayis.read());
746        }
747
748        byte[] buffer2 = new byte[] { 'a', 'b', 'c', 'd' };
749        buffos.write(buffer2, 0, 4);
750        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
751        assertEquals("Bytes written, not buffered", 10, byteArrayis.available());
752        buffos.flush();
753        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
754        assertEquals("Bytes not written after flush", 14, byteArrayis
755                .available());
756        for (int i = 0; i < 10; i++) {
757            assertEquals(buffer[i], byteArrayis.read());
758        }
759        for (int i = 0; i < 4; i++) {
760            assertEquals(buffer2[i], byteArrayis.read());
761        }
762
763        byte[] buffer3 = new byte[] { 'e', 'f', 'g', 'h', 'i' };
764        buffos.write(buffer3, 0, 5);
765        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
766        assertEquals("Bytes written, not buffered", 14, byteArrayis.available());
767        buffos.flush();
768        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
769        assertEquals("Bytes not written after flush", 19, byteArrayis
770                .available());
771        for (int i = 0; i < 10; i++) {
772            assertEquals(buffer[i], byteArrayis.read());
773        }
774        for (int i = 0; i < 4; i++) {
775            assertEquals(buffer2[i], byteArrayis.read());
776        }
777        for (int i = 0; i < 5; i++) {
778            assertEquals(buffer3[i], byteArrayis.read());
779        }
780
781        buffos.write(new byte[] { 'j', 'k' });
782        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
783        assertEquals("Bytes written, not buffered", 19, byteArrayis.available());
784        buffos.flush();
785        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
786        assertEquals("Bytes not written after flush", 21, byteArrayis
787                .available());
788
789        buffos.close();
790    }
791
792    public void test_write_Scenario3() throws IOException {
793        ByteArrayOutputStream byteArrayos = new ByteArrayOutputStream();
794        ByteArrayInputStream byteArrayis = null;
795        byte[] buffer = "1234567890".getBytes("UTF-8");
796
797        BufferedOutputStream buffos = new BufferedOutputStream(byteArrayos, 5);
798        buffos.write(buffer, 0, 4);
799        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
800        assertEquals("Bytes written, not buffered", 0, byteArrayis.available());
801        buffos.flush();
802        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
803        assertEquals("Bytes not written after flush", 4, byteArrayis
804                .available());
805        for (int i = 0; i < 4; i++) {
806            assertEquals(buffer[i], byteArrayis.read());
807        }
808
809        buffos.write(buffer, 0, 5);
810        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
811        assertEquals("Bytes written, not buffered", 9, byteArrayis.available());
812        buffos.flush();
813        byteArrayis = new ByteArrayInputStream(byteArrayos.toByteArray());
814        assertEquals("Bytes not written after flush", 9, byteArrayis
815                .available());
816        for (int i = 0; i < 4; i++) {
817            assertEquals(buffer[i], byteArrayis.read());
818        }
819        for (int i = 0; i < 5; i++) {
820            assertEquals(buffer[i], byteArrayis.read());
821        }
822    }
823
824    // Regression test for flush on closed stream
825    public void test_flush_on_closed_stream() throws IOException {
826        BufferedOutputStream bos = new BufferedOutputStream(new ByteArrayOutputStream());
827        bos.close();
828        try {
829            bos.flush(); // RI does not throw exception
830        } catch (IOException expected) { // but Android does
831        }
832    }
833
834    /**
835     * Tears down the fixture, for example, close a network connection. This
836     * method is called after a test is executed.
837     */
838    protected void tearDown() throws IOException {
839        if (bais != null) {
840            bais.close();
841        }
842        if (os != null) {
843            os.close();
844        }
845        if (baos != null) {
846            baos.close();
847        }
848    }
849}
850