1 /*
   2  * Copyright (c) 2018, 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 4734607 4767225 4863613
  27  * @summary Decode a file using EUC-TW, test for decode errors
  28  * @modules jdk.charsets
  29  * @run main EUCTWBufferBoundaryDecodeTest
  30  */
  31 import java.io.BufferedReader;
  32 import java.io.BufferedWriter;
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.File;
  35 import java.io.FileInputStream;
  36 import java.io.InputStreamReader;
  37 import java.io.OutputStreamWriter;
  38 
  39 /*
  40  * Tests for decode errors in NIO EUC-TW decoder. 4734607 details
  41  * decoding errors which occur when the input file > 8k in size
  42  * and contains numerous US-ASCII range chars
  43  */
  44 public class EUCTWBufferBoundaryDecodeTest {
  45 
  46     private static final String inputFileName
  47             = System.getProperty("os.name").startsWith("Windows")
  48             ? "tradChinese.win.po"
  49             : "tradChinese.po";
  50 
  51     public static void main(String[] args) throws Exception {
  52         ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 120);
  53 
  54         File inputFile = new File(System.getProperty("test.src"), inputFileName);
  55         FileInputStream fis = new FileInputStream(inputFile);
  56 
  57         // Decode the EUC_TW source file, re-encode it -- (roundtrip)
  58         // input and output files ought to be byte for byte identical
  59         BufferedReader bin
  60                 = new BufferedReader(new InputStreamReader(fis, "EUC_TW"));
  61         BufferedWriter bout
  62                 = new BufferedWriter(new OutputStreamWriter(bos, "EUC-TW"));
  63 
  64         String line = bin.readLine();
  65 
  66         while (line != null) {
  67             bout.write(line);
  68             bout.newLine();
  69             line = bin.readLine();
  70         }
  71 
  72         bout.close();
  73         bin.close();
  74 
  75         // Compare the output with the expected output byte by byte
  76         byte[] outputBytes = bos.toByteArray();
  77         int i = 0;
  78         FileInputStream fi = new FileInputStream(inputFile);
  79 
  80         while (i < outputBytes.length) {
  81             byte b = (byte) fi.read();
  82             if (b != outputBytes[i++]) {
  83                 throw new Exception("bug 4734607: test failed");
  84             }
  85         }
  86         fi.close();
  87     }
  88 }