156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes/* Licensed to the Apache Software Foundation (ASF) under one or more 256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * contributor license agreements. See the NOTICE file distributed with 356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * this work for additional information regarding copyright ownership. 456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * (the "License"); you may not use this file except in compliance with 656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * the License. You may obtain a copy of the License at 75ec6bf8d033754e06a463adb091d2c0afd0755acElliott Hughes * 856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 95ec6bf8d033754e06a463adb091d2c0afd0755acElliott Hughes * 1056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * Unless required by applicable law or agreed to in writing, software 1156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * See the License for the specific language governing permissions and 1456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * limitations under the License. 1556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes */ 1656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 17e5fea3d504609d22337a5311d3ce0e72314bceeeNarayan Kamathpackage org.apache.harmony.tests.java.nio.charset; 1856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 1956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.io.UnsupportedEncodingException; 2056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.ByteBuffer; 2156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.CharBuffer; 2256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CharacterCodingException; 2356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.Charset; 2456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CoderResult; 2556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughesimport java.nio.charset.CodingErrorAction; 2656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 2756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes/** 285ec6bf8d033754e06a463adb091d2c0afd0755acElliott Hughes * 2956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes */ 3056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughespublic class UTF16CharsetDecoderTest extends CharsetDecoderTest { 3156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 3256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes boolean bigEndian = true; 3356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 3456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes protected void setUp() throws Exception { 3556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes cs = Charset.forName("utf-16"); 3656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bom = "\ufeff"; 3756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes super.setUp(); 3856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 3956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 4056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes /* 4156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes * @see CharsetDecoderTest#tearDown() 4256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes */ 4356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes protected void tearDown() throws Exception { 4456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes super.tearDown(); 4556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 4656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 4756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes protected ByteBuffer getByteBuffer() { 4856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // FIXME: different here 4956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // if don't specified BOM 5056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // ICU default is LE 5156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // JDK default is BE 5256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 5356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // maybe start with 0xFEFF, which means big endian 5456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // 0xFFFE, which means little endian 5556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes byte[] b = (bigEndian) ? new byte[] { -1, -2, 32, 0, 98, 0, 117, 0, 5656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 102, 0, 102, 0, 101, 0, 114, 0 } : new byte[] { -2, -1, 0, 32, 5756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 0, 98, 0, 117, 0, 102, 0, 102, 0, 101, 0, 114 }; 5856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes return ByteBuffer.wrap(b); 5956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 6056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 6156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes protected ByteBuffer getHeadlessByteBuffer() { 6256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes ByteBuffer b = getByteBuffer(); 6356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes b.position(2); 6456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes byte[] bytes = new byte[b.remaining()]; 6556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes b.get(bytes); 6656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes return ByteBuffer.wrap(bytes); 6756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 6856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 6956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes public void testLittleEndianByteBufferCharBuffer() 7056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes throws CharacterCodingException, UnsupportedEncodingException { 7156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = false; 7256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes implTestDecodeByteBufferCharBuffer(getByteBuffer()); 7356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = true; 7456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 7556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 7656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes public void testLittleEndianReadOnlyByteBufferCharBuffer() 7756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes throws CharacterCodingException, UnsupportedEncodingException { 7856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = false; 7956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes implTestDecodeByteBufferCharBuffer(getByteBuffer().asReadOnlyBuffer()); 8056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = true; 8156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 8256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 8356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes public void testLittleEndian() throws CharacterCodingException, 8456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes UnsupportedEncodingException { 8556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = false; 8656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes implTestDecodeByteBuffer(); 8756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes bigEndian = true; 8856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 8956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 9056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // FIXME: give up this tests 9156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // public void testDefaultCharsPerByte() { 9256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // // assertEquals(1, decoder.averageCharsPerByte()); 9356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // // assertEquals(1, decoder.maxCharsPerByte()); 9456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // assertEquals(decoder.averageCharsPerByte(), 0.5, 0.001); 9556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // assertEquals(decoder.maxCharsPerByte(), 2, 0.001); 9656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // } 9756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 9856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes ByteBuffer getUnmappedByteBuffer() throws UnsupportedEncodingException { 9956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes return null; 10056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 10156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 10256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes ByteBuffer getMalformedByteBuffer() throws UnsupportedEncodingException { 10356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes return null; 10456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // FIXME: different here, RI can parse 0xd8d8 10556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // ByteBuffer buffer = ByteBuffer.allocate(100); 10656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.put((byte) -1); 10756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.put((byte) -2); 10856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.put((byte) 0xdc); 10956ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.put((byte) 0xdc); 11056ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.put(unibytes); 11156ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // buffer.flip(); 11256ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes // return buffer; 11356ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 11456ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes 11556ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes ByteBuffer getExceptionByteArray() throws UnsupportedEncodingException { 11656ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes return null; 11756ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes } 11856ddb0af9c75dca21f10cd26e73b9f301c58771eElliott Hughes} 119