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