author | alanb |
Fri, 14 Sep 2018 16:56:09 +0100 | |
changeset 51745 | 90c1dcdebc64 |
parent 48252 | 77b88d8f8380 |
permissions | -rw-r--r-- |
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
1 |
/* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
4 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
8 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
13 |
* accompanied this code). |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
14 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
18 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
21 |
* questions. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
22 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
23 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
24 |
import java.io.ByteArrayOutputStream; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
25 |
import java.io.FileInputStream; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
26 |
import java.io.FileOutputStream; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
27 |
import java.io.IOException; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
28 |
import java.io.Reader; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
29 |
import java.io.Writer; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
30 |
import java.nio.channels.Channels; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
31 |
import java.nio.channels.ReadableByteChannel; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
32 |
import java.nio.channels.WritableByteChannel; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
33 |
import java.nio.charset.Charset; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
34 |
import java.nio.charset.MalformedInputException; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
35 |
import java.nio.charset.StandardCharsets; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
36 |
import java.nio.file.Paths; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
37 |
import org.testng.Assert; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
38 |
import org.testng.annotations.DataProvider; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
39 |
import org.testng.annotations.Test; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
40 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
41 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
42 |
* @test |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
43 |
* @bug 8183743 |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
44 |
* @summary Test to verify the new overload method with Charset functions the same |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
45 |
* as the existing method that takes a charset name. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
46 |
* @run testng EncodingTest |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
47 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
48 |
public class EncodingTest { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
49 |
static final int ITERATIONS = 2; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
50 |
public static final String CS_UTF8 = StandardCharsets.UTF_8.name(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
51 |
public static final String CS_ISO8859 = StandardCharsets.ISO_8859_1.name(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
52 |
static String USER_DIR = System.getProperty("user.dir", "."); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
53 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
54 |
// malformed input: a high surrogate without the low surrogate |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
55 |
static char[] illChars = { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
56 |
'\u00fa', '\ud800' |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
57 |
}; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
58 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
59 |
static byte[] data = getData(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
60 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
61 |
static byte[] getData() { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
62 |
try { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
63 |
String str1 = "A string that contains "; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
64 |
String str2 = " , an invalid character for UTF-8."; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
65 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
66 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
67 |
baos.write(str1.getBytes()); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
68 |
baos.write(0xFA); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
69 |
baos.write(str2.getBytes()); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
70 |
return baos.toByteArray(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
71 |
} catch (IOException ex) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
72 |
return null; //shouldn't happen |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
73 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
74 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
75 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
76 |
String testFile = Paths.get(USER_DIR, "channelsEncodingTest.txt").toString(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
77 |
String testIllegalInput = Paths.get(USER_DIR, "channelsIllegalInputTest.txt").toString(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
78 |
String testIllegalOutput = Paths.get(USER_DIR, "channelsIllegalOutputTest.txt").toString(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
79 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
80 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
81 |
/* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
82 |
* DataProvider for read and write test. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
83 |
* Writes and reads with the same encoding |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
84 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
85 |
@DataProvider(name = "writeAndRead") |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
86 |
public Object[][] getWRParameters() { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
87 |
return new Object[][]{ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
88 |
{testFile, StandardCharsets.ISO_8859_1.name(), null, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
89 |
StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
90 |
{testFile, null, StandardCharsets.ISO_8859_1, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
91 |
StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
92 |
{testFile, StandardCharsets.UTF_8.name(), null, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
93 |
StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
94 |
{testFile, null, StandardCharsets.UTF_8, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
95 |
StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
96 |
}; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
97 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
98 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
99 |
/* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
100 |
* DataProvider for illegal input test |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
101 |
* Writes the data in ISO8859 and reads with UTF8, expects MalformedInputException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
102 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
103 |
@DataProvider(name = "illegalInput") |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
104 |
public Object[][] getParameters() { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
105 |
return new Object[][]{ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
106 |
{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, StandardCharsets.UTF_8.name(), null}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
107 |
{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, null, StandardCharsets.UTF_8}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
108 |
{testIllegalInput, null, StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8.name(), null}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
109 |
{testIllegalInput, null, StandardCharsets.ISO_8859_1, null, StandardCharsets.UTF_8}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
110 |
}; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
111 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
112 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
113 |
/* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
114 |
* DataProvider for illegal output test |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
115 |
* Attemps to write some malformed chars, expects MalformedInputException |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
116 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
117 |
@DataProvider(name = "illegalOutput") |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
118 |
public Object[][] getWriteParameters() { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
119 |
return new Object[][]{ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
120 |
{testIllegalOutput, StandardCharsets.UTF_8.name(), null}, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
121 |
{testIllegalOutput, null, StandardCharsets.UTF_8} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
122 |
}; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
123 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
124 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
125 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
126 |
* Verifies that the Readers created with the following methods are |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
127 |
* equivalent: |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
128 |
* newReader(ReadableByteChannel ch, String csName) |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
129 |
* newReader(ReadableByteChannel ch, Charset charset) |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
130 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
131 |
* The verification follows the following steps: |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
132 |
* Writes a file with a writer created with the specified charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
133 |
* Reads it with a reader created with newReader using the same charset; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
134 |
* Compares that the results are the same. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
135 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
136 |
* @param file the file name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
137 |
* @param csnWriter the charset name for creating the writer |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
138 |
* @param charsetWriter the charset for creating the writer |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
139 |
* @param csnReader the charset name for creating the reader |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
140 |
* @param charsetReader the charset for creating the reader |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
141 |
* @throws Exception |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
142 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
143 |
@Test(dataProvider = "writeAndRead") |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
144 |
public void testWriteAndRead(String file, String csnWriter, Charset charsetWriter, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
145 |
String csnReader, Charset charsetReader) throws Exception { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
146 |
writeToFile(data, file, csnWriter, charsetWriter); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
147 |
// read using charset name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
148 |
String result1 = readFileToString(file, csnReader, null); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
149 |
String result2 = readFileToString(file, null, charsetReader); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
150 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
151 |
Assert.assertEquals(result1, result2); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
152 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
153 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
154 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
155 |
* Verifies that MalformedInputException is thrown when an input byte sequence |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
156 |
* is illegal for given charset that is configured for the reader. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
157 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
158 |
* @param file the file to be read |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
159 |
* @param csnWriter the charset name for creating the writer |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
160 |
* @param charsetWriter the charset for creating the writer |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
161 |
* @param csnReader the charset name for creating the reader |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
162 |
* @param charsetReader the charset for creating the reader |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
163 |
* @throws Exception |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
164 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
165 |
@Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class) |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
166 |
void testMalformedInput(String file, String csnWriter, Charset charsetWriter, |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
167 |
String csnReader, Charset charsetReader) throws Exception { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
168 |
writeToFile(data, file, csnWriter, charsetWriter); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
169 |
readFileToString(file, csnReader, charsetReader); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
170 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
171 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
172 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
173 |
* Attempts to write illegal characters using a writer created by calling |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
174 |
* the newWriter method and expects a MalformedInputException. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
175 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
176 |
* @param fileName the file name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
177 |
* @param csn the charset name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
178 |
* @param charset the charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
179 |
* @throws Exception |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
180 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
181 |
@Test(dataProvider = "illegalOutput", expectedExceptions = MalformedInputException.class) |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
182 |
public void testMalformedOutput(String fileName, String csn, Charset charset) |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
183 |
throws Exception { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
184 |
try (FileOutputStream fos = new FileOutputStream(fileName); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
185 |
WritableByteChannel wbc = (WritableByteChannel) fos.getChannel();) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
186 |
Writer writer; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
187 |
if (csn != null) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
188 |
writer = Channels.newWriter(wbc, csn); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
189 |
} else { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
190 |
writer = Channels.newWriter(wbc, charset); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
191 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
192 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
193 |
for (int i = 0; i < ITERATIONS; i++) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
194 |
writer.write(illChars); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
195 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
196 |
writer.flush(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
197 |
writer.close(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
198 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
199 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
200 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
201 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
202 |
* Writes the data to a file using a writer created by calling the newWriter |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
203 |
* method. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
204 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
205 |
* @param data the data to be written |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
206 |
* @param fileName the file name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
207 |
* @param csn the charset name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
208 |
* @param charset the charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
209 |
* @throws Exception |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
210 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
211 |
private void writeToFile(byte[] data, String fileName, String csn, Charset charset) throws Exception { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
212 |
try (FileOutputStream fos = new FileOutputStream(fileName); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
213 |
WritableByteChannel wbc = (WritableByteChannel) fos.getChannel()) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
214 |
Writer writer; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
215 |
String temp; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
216 |
if (csn != null) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
217 |
writer = Channels.newWriter(wbc, csn); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
218 |
temp = new String(data, csn); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
219 |
} else { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
220 |
writer = Channels.newWriter(wbc, charset); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
221 |
temp = new String(data, charset); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
222 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
223 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
224 |
for (int i = 0; i < ITERATIONS; i++) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
225 |
writer.write(temp); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
226 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
227 |
writer.flush(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
228 |
writer.close(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
229 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
230 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
231 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
232 |
/** |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
233 |
* Reads a file into a String. |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
234 |
* |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
235 |
* @param file the file to be read |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
236 |
* @param csn the charset name |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
237 |
* @param charset the charset |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
238 |
* @throws Exception |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
239 |
*/ |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
240 |
String readFileToString(String file, String csn, Charset charset) throws Exception { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
241 |
String result; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
242 |
try (FileInputStream fis = new FileInputStream(file); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
243 |
ReadableByteChannel rbc = (ReadableByteChannel) fis.getChannel()) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
244 |
Reader reader; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
245 |
if (csn != null) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
246 |
reader = Channels.newReader(rbc, csn); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
247 |
} else { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
248 |
reader = Channels.newReader(rbc, charset); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
249 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
250 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
251 |
int messageSize = data.length * ITERATIONS; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
252 |
char data1[] = new char[messageSize]; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
253 |
int totalRead = 0; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
254 |
int charsRead = 0; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
255 |
while (totalRead < messageSize) { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
256 |
totalRead += charsRead; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
257 |
charsRead = reader.read(data1, totalRead, messageSize - totalRead); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
258 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
259 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
260 |
result = new String(data1, 0, totalRead); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
261 |
reader.close(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
262 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
263 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
264 |
return result; |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
265 |
} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff
changeset
|
266 |
} |