19559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes/*
29559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * Copyright (C) 2010 The Android Open Source Project
39559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes *
49559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
59559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * you may not use this file except in compliance with the License.
69559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * You may obtain a copy of the License at
79559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes *
89559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
99559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes *
109559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * Unless required by applicable law or agreed to in writing, software
119559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
129559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * See the License for the specific language governing permissions and
149559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes * limitations under the License.
159559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes */
169559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
179559e748729ef1deb6400f31d0407543cbff3566Elliott Hughespackage libcore.java.io;
189559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
199559e748729ef1deb6400f31d0407543cbff3566Elliott Hughesimport java.io.ByteArrayOutputStream;
209559e748729ef1deb6400f31d0407543cbff3566Elliott Hughesimport java.io.DataOutputStream;
219559e748729ef1deb6400f31d0407543cbff3566Elliott Hughesimport java.io.UTFDataFormatException;
229559e748729ef1deb6400f31d0407543cbff3566Elliott Hughesimport java.util.Arrays;
239559e748729ef1deb6400f31d0407543cbff3566Elliott Hughesimport junit.framework.TestCase;
249559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
259559e748729ef1deb6400f31d0407543cbff3566Elliott Hughespublic final class DataOutputStreamTest extends TestCase {
269559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
279559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    private DataOutputStream os = new DataOutputStream(bytes);
289559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
299559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeBoolean() throws Exception {
309559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeBoolean(true);
319559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeBoolean(false);
329559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[01, 00]", toHexString(bytes.toByteArray()));
339559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
349559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
359559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeByte() throws Exception {
369559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeByte(-1);
379559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeByte(0);
389559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeByte(1);
399559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeByte(129);
409559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // writeByte takes only the bottom byte from its int parameter.
419559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeByte(0x1234);
429559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[ff, 00, 01, 81, 34]", toHexString(bytes.toByteArray()));
439559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
449559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
459559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeBytes() throws Exception {
469559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // writeBytes takes only the bottom byte from each character.
479559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeBytes("0\u12341");
489559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[30, 34, 31]", toHexString(bytes.toByteArray()));
499559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
509559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
519559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeChar() throws Exception {
529559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // writeChar writes two-byte big-endian characters.
539559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeChar('0');
549559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeChar(0x1234);
559559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[00, 30, 12, 34]", toHexString(bytes.toByteArray()));
569559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
579559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
589559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeChars() throws Exception {
599559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // writeChars writes two-byte big-endian characters.
609559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeChars("0\u12341");
619559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[00, 30, 12, 34, 00, 31]", toHexString(bytes.toByteArray()));
629559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
639559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
649559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeDouble() throws Exception {
659559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeDouble(Double.longBitsToDouble(0x0123456789abcdefL));
669559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[01, 23, 45, 67, 89, ab, cd, ef]", toHexString(bytes.toByteArray()));
679559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
689559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
699559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeFloat() throws Exception {
709559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeFloat(Float.intBitsToFloat(0x01234567));
719559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[01, 23, 45, 67]", toHexString(bytes.toByteArray()));
729559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
739559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
749559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeInt() throws Exception {
759559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeInt(0x01234567);
769559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[01, 23, 45, 67]", toHexString(bytes.toByteArray()));
779559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
789559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
799559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeLong() throws Exception {
809559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeLong(0x0123456789abcdefL);
819559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[01, 23, 45, 67, 89, ab, cd, ef]", toHexString(bytes.toByteArray()));
829559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
839559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
849559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeShort() throws Exception {
859559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // writeShort only writes the bottommost 16 bits of its int parameter.
869559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeShort(0x01234567);
879559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[45, 67]", toHexString(bytes.toByteArray()));
889559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
899559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
909559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeUTF() throws Exception {
919559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // The limit is 65535 *bytes* but we want to test 2- and 3-byte characters too.
929559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        char[] chars = new char[65535 - 1 - 2];
939559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        Arrays.fill(chars, 0, chars.length, 'a');
949559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        chars[0] = '\u0666'; // a two-byte character
959559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        chars[1] = '\u2603'; // a three-byte character
969559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        String maxLength = new String(chars);
979559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeUTF(maxLength);
989559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        byte[] expected = new byte[2 + 65535];
999559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[0] = (byte) 0xff;
1009559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[1] = (byte) 0xff;
1019559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // U+0666 = 0xD9 0xA6
1029559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[2] = (byte) 0xd9;
1039559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[3] = (byte) 0xa6;
1049559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // U+2603 = 0xE2 0x98 0x83
1059559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[4] = (byte) 0xe2;
1069559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[5] = (byte) 0x98;
1079559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        expected[6] = (byte) 0x83;
1089559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        Arrays.fill(expected, 7, expected.length, (byte) 'a');
1099559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals(Arrays.toString(expected), Arrays.toString(bytes.toByteArray()));
1109559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
1119559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
1129559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeUTF_NUL() throws Exception {
1139559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // This is a special case, represented with two non-zero bytes.
1149559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        os.writeUTF("\u0000");
1159559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[00, 02, c0, 80]", toHexString(bytes.toByteArray()));
1169559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
1179559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
1189559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public void test_writeUTF_too_long() throws Exception {
1199559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        String tooLong = new String(new char[65536]);
1209559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        try {
1219559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            os.writeUTF(tooLong);
1229559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            fail("should throw UTFDataFormatException");
1239559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        } catch (UTFDataFormatException expected) {
1249559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        }
1259559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        assertEquals("[]", toHexString(bytes.toByteArray()));
1269559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
1279559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes
1289559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    /**
1299559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes     * Returns a string representation of a byte array that's more useful for debugging.
1309559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes     * TODO: move this somewhere where it's available to all tests.
1319559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes     */
1329559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    public static String toHexString(byte[] array) {
1339559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        if (array == null) {
1349559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            return null;
1359559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        }
1369559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        if (array.length == 0) {
1379559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            return "[]";
1389559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        }
1399559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        StringBuilder sb = new StringBuilder();
1409559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        sb.append('[');
1419559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        // TODO: don't use String.format if we put this in the library. Too expensive!
1429559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        sb.append(String.format("%02x", array[0] & 0xff));
1439559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        for (int i = 1; i < array.length; i++) {
1449559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            sb.append(", ");
1459559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes            sb.append(String.format("%02x", array[i] & 0xff));
1469559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        }
1479559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        sb.append(']');
1489559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes        return sb.toString();
1499559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes    }
1509559e748729ef1deb6400f31d0407543cbff3566Elliott Hughes}
151