1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6995537
  27  * @summary check OutputStreamWriter and PrintStream flush the underlying
  28  *          charset encoder correctly
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 
  34 public class Flush {
  35     public static void main(String args[]) throws Exception {
  36 
  37         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  38         String str = "\u6700";
  39         byte[] bytes = new byte[] { (byte)0x1b, (byte)0x24, (byte)0x42, 
  40                                     (byte)0x3a, (byte)0x47, 
  41                                     (byte)0x1b, (byte)0x28, (byte)0x42 };
  42 
  43         OutputStreamWriter osw = new OutputStreamWriter(bos, "iso-2022-jp");
  44         osw.write(str, 0, str.length());
  45         osw.flush();
  46         check(bos.toByteArray(), bytes);
  47 
  48         bos.reset();
  49         PrintStream ps = new PrintStream(bos, false, "iso-2022-jp");
  50         ps.print(str);
  51         check(bos.toByteArray(), bytes);
  52 
  53      }
  54 
  55      private static void check(byte[] result, byte[] expected)
  56          throws Exception
  57      {
  58         if (!Arrays.equals(result, expected)) {
  59             System.out.println("Expected:");
  60             for (int i = 0; i < expected.length; i++)
  61                 System.out.printf("0x%x ", expected[i] & 0xff);
  62             System.out.println();
  63             System.out.println("Result::");
  64             for (int i = 0; i < result.length; i++)
  65                 System.out.printf("0x%x ", result[i] & 0xff);
  66             System.out.println();
  67             throw new RuntimeException("Test failed");
  68         }
  69 
  70     }
  71 
  72  
  73 }