171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes/* 271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * Copyright (C) 2009 The Android Open Source Project 371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * 471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * you may not use this file except in compliance with the License. 671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * You may obtain a copy of the License at 771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * 871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * 1071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * See the License for the specific language governing permissions and 1471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes * limitations under the License. 1571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes */ 1671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 1771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughespackage java.nio.charset; 1871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 1971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughesimport java.nio.ByteBuffer; 2071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughesimport java.nio.CharBuffer; 2171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 2271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughesimport junit.framework.Test; 2371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughesimport junit.framework.TestSuite; 2471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 2571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughespublic class CharsetDecoderTest extends junit.framework.TestCase { 2671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes private static final String CHARSET = "UTF-16"; 2771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 2871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes private static final String SAMPLE_STRING = "Android"; 2971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 3071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 3171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes public void test_ByteArray_decode_no_offset() throws Exception { 3271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 3371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 3471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice(); 3571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharBuffer outBuffer = CharBuffer.allocate(arr.length); 3671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes decoder.reset(); 3771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true); 3871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes assertFalse(coderResult.toString(), coderResult.isError()); 3971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes decoder.flush(outBuffer); 4071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes outBuffer.flip(); 4171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 4271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 4371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 4471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 4571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes public void test_ByteArray_decode_with_offset() throws Exception { 4671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 4771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 4871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes arr = prependByteToByteArray(arr, new Integer(1).byteValue()); 4971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes int offset = 1; 5071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice(); 5171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset); 5271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes decoder.reset(); 5371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true); 5471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes assertFalse(coderResult.toString(), coderResult.isError()); 5571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes decoder.flush(outBuffer); 5671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes outBuffer.flip(); 5771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 5871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 5971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 6071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes // http://code.google.com/p/android/issues/detail?id=4237 6171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes public void test_ByteArray_decode_with_offset_using_facade_method() throws Exception { 6271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharsetDecoder decoder = getCharsetDecoderUnderTest(); 6371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes byte[] arr = getEncodedByteArrayFixture(); 6471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes arr = prependByteToByteArray(arr, new Integer(1).byteValue()); 6571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes int offset = 1; 6671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharBuffer outBuffer = decoder.decode(ByteBuffer.wrap(arr, offset, arr.length - offset)); 6771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes assertEquals(SAMPLE_STRING, outBuffer.toString().trim()); 6871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 6971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 7071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes private static byte[] prependByteToByteArray(byte[] arr, byte b) { 7171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes byte[] result = new byte[arr.length + 1]; 7271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes result[0] = b; 7371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes System.arraycopy(arr, 0, result, 1, arr.length); 7471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes return result; 7571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 7671684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 7771684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes private static CharsetDecoder getCharsetDecoderUnderTest() { 7871684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes return Charset.forName(CHARSET).newDecoder(); 7971684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 8071684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes 8171684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes private byte[] getEncodedByteArrayFixture() throws CharacterCodingException { 8271684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes CharsetEncoder encoder = Charset.forName(CHARSET).newEncoder(); 8371684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes return encoder.encode(CharBuffer.wrap(SAMPLE_STRING)).array(); 8471684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes } 8571684d91a50ae210cec2cb8eccdc632bc494c786Elliott Hughes} 86