796
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
|
796
|
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 |
*
|
5506
|
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.
|
796
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4251646
|
|
26 |
@summary Make sure buffer boundary convert works
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.nio.*;
|
|
30 |
import java.nio.charset.*;
|
|
31 |
|
|
32 |
public class TestISO2022JPEncoder {
|
|
33 |
static char[] inputChars = {'\u0020', '\u0020', '\u0020', '\u0020',
|
|
34 |
'\u0020', '\u0020', '\u0020', '\u0020',
|
|
35 |
'\u0020', '\u4e00'};
|
|
36 |
static byte[] expectedBytes1 = {0x20, 0x20, 0x20, 0x20, 0x20,
|
|
37 |
0x20, 0x20, 0x20, 0x20};
|
|
38 |
static byte[] expectedBytes2 = {0x1b, 0x24, 0x42, 0x30, 0x6c,
|
|
39 |
0x1b, 0x28, 0x42};
|
|
40 |
static byte[] outputBuff = new byte[10];
|
|
41 |
|
|
42 |
public static void main(String args[]) throws Exception {
|
|
43 |
CharsetEncoder enc = Charset.forName("ISO2022JP").newEncoder();
|
|
44 |
CharBuffer cb = CharBuffer.wrap(inputChars);
|
|
45 |
ByteBuffer bb = ByteBuffer.wrap(outputBuff);
|
|
46 |
CoderResult cr = enc.encode(cb, bb, false);
|
|
47 |
if (!cr.isOverflow())
|
|
48 |
throw new Exception("Expected CodeResult.OVERFLOW was not returned");
|
|
49 |
for (int i = 0; i < expectedBytes1.length; ++i) {
|
|
50 |
//System.out.println(expectedBytes1[i] + ":" + outputBuff[i]);
|
|
51 |
if (expectedBytes1[i] != outputBuff[i]) {
|
|
52 |
throw new Exception("Output bytes does not match at first conversion");
|
|
53 |
}
|
|
54 |
}
|
|
55 |
int nci = cb.position();
|
|
56 |
if (nci != expectedBytes1.length)
|
|
57 |
throw new Exception("Output length does not match at first conversion");
|
|
58 |
bb.clear();
|
|
59 |
cr = enc.encode(cb, bb, true);
|
|
60 |
enc.flush(bb);
|
|
61 |
//System.out.println(ret + "," + expectedBytes2.length);
|
|
62 |
bb.flip();
|
|
63 |
int len = bb.remaining();
|
|
64 |
if (len != expectedBytes2.length)
|
|
65 |
throw new Exception("Output length does not match at second conversion");
|
|
66 |
for (int i = 0; i < expectedBytes2.length; ++i) {
|
|
67 |
//System.out.println(expectedBytes2[i] + ":" + outputBuff[i]);
|
|
68 |
if (expectedBytes2[i] != outputBuff[i]) {
|
|
69 |
throw new Exception("Output bytes does not match at second conversion");
|
|
70 |
}
|
|
71 |
}
|
|
72 |
}
|
|
73 |
}
|