1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.utils;
18
19import java.io.ByteArrayOutputStream;
20import java.io.DataOutputStream;
21import java.io.IOException;
22import java.io.OutputStream;
23
24/** Extends {@link DataOutputStream} with additional convenience methods.
25 * @author Nathan Sweet */
26public class DataOutput extends DataOutputStream {
27	public DataOutput (OutputStream out) {
28		super(out);
29	}
30
31	/** Writes a 1-5 byte int.
32	 * @param optimizePositive If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
33	 *           inefficient (5 bytes). */
34	public int writeInt (int value, boolean optimizePositive) throws IOException {
35		if (!optimizePositive) value = (value << 1) ^ (value >> 31);
36		if (value >>> 7 == 0) {
37			write((byte)value);
38			return 1;
39		}
40		write((byte)((value & 0x7F) | 0x80));
41		if (value >>> 14 == 0) {
42			write((byte)(value >>> 7));
43			return 2;
44		}
45		write((byte)(value >>> 7 | 0x80));
46		if (value >>> 21 == 0) {
47			write((byte)(value >>> 14));
48			return 3;
49		}
50		write((byte)(value >>> 14 | 0x80));
51		if (value >>> 28 == 0) {
52			write((byte)(value >>> 21));
53			return 4;
54		}
55		write((byte)(value >>> 21 | 0x80));
56		write((byte)(value >>> 28));
57		return 5;
58	}
59
60	/** Writes a length and then the string as UTF8.
61	 * @param value May be null. */
62	public void writeString (String value) throws IOException {
63		if (value == null) {
64			write(0);
65			return;
66		}
67		int charCount = value.length();
68		if (charCount == 0) {
69			writeByte(1);
70			return;
71		}
72		writeInt(charCount + 1, true);
73		// Try to write 8 bit chars.
74		int charIndex = 0;
75		for (; charIndex < charCount; charIndex++) {
76			int c = value.charAt(charIndex);
77			if (c > 127) break;
78			write((byte)c);
79		}
80		if (charIndex < charCount) writeString_slow(value, charCount, charIndex);
81	}
82
83	private void writeString_slow (String value, int charCount, int charIndex) throws IOException {
84		for (; charIndex < charCount; charIndex++) {
85			int c = value.charAt(charIndex);
86			if (c <= 0x007F) {
87				write((byte)c);
88			} else if (c > 0x07FF) {
89				write((byte)(0xE0 | c >> 12 & 0x0F));
90				write((byte)(0x80 | c >> 6 & 0x3F));
91				write((byte)(0x80 | c & 0x3F));
92			} else {
93				write((byte)(0xC0 | c >> 6 & 0x1F));
94				write((byte)(0x80 | c & 0x3F));
95			}
96		}
97	}
98}
99