aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBitstr.java
blob: 97897fe182c9e19691d90be0bf077b87693d6fa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2007-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
package com.ericsson.otp.erlang;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Provides a Java representation of Erlang bitstrs. An Erlang bitstr is an
 * Erlang binary with a length not an integral number of bytes (8-bit). Anything
 * can be represented as a sequence of bytes can be made into an Erlang bitstr.
 */
public class OtpErlangBitstr extends OtpErlangObject implements Serializable,
	Cloneable {
    // don't change this!
    static final long serialVersionUID = -3781009633593609217L;

    protected byte[] bin;
    protected int pad_bits;

    /**
     * Create a bitstr from a byte array
     * 
     * @param bin
     *                the array of bytes from which to create the bitstr.
     */
    public OtpErlangBitstr(final byte[] bin) {
	this.bin = new byte[bin.length];
	System.arraycopy(bin, 0, this.bin, 0, bin.length);
	pad_bits = 0;
    }

    /**
     * Create a bitstr with pad bits from a byte array.
     * 
     * @param bin
     *                the array of bytes from which to create the bitstr.
     * @param pad_bits
     *                the number of unused bits in the low end of the last byte.
     */
    public OtpErlangBitstr(final byte[] bin, final int pad_bits) {
	this.bin = new byte[bin.length];
	System.arraycopy(bin, 0, this.bin, 0, bin.length);
	this.pad_bits = pad_bits;

	check_bitstr(this.bin, this.pad_bits);
    }

    private void check_bitstr(final byte[] bin, final int pad_bits) {
	if (pad_bits < 0 || 7 < pad_bits) {
	    throw new java.lang.IllegalArgumentException(
		    "Padding must be in range 0..7");
	}
	if (pad_bits != 0 && bin.length == 0) {
	    throw new java.lang.IllegalArgumentException(
		    "Padding on zero length bitstr");
	}
	if (bin.length != 0) {
	    // Make sure padding is zero
	    bin[bin.length - 1] &= ~((1 << pad_bits) - 1);
	}
    }

    /**
     * Create a bitstr from a stream containing a bitstr encoded in Erlang
     * external format.
     * 
     * @param buf
     *                the stream containing the encoded bitstr.
     * 
     * @exception OtpErlangDecodeException
     *                    if the buffer does not contain a valid external
     *                    representation of an Erlang bitstr.
     */
    public OtpErlangBitstr(final OtpInputStream buf)
	    throws OtpErlangDecodeException {
	final int pbs[] = { 0 }; // This is ugly just to get a value-result
				    // parameter
	bin = buf.read_bitstr(pbs);
	pad_bits = pbs[0];

	check_bitstr(bin, pad_bits);
    }

    /**
     * Create a bitstr from an arbitrary Java Object. The object must implement
     * java.io.Serializable or java.io.Externalizable.
     * 
     * @param o
     *                the object to serialize and create this bitstr from.
     */
    public OtpErlangBitstr(final Object o) {
	try {
	    bin = toByteArray(o);
	    pad_bits = 0;
	} catch (final IOException e) {
	    throw new java.lang.IllegalArgumentException(
		    "Object must implement Serializable");
	}
    }

    private static byte[] toByteArray(final Object o)
	    throws java.io.IOException {

	if (o == null) {
	    return null;
	}

	/* need to synchronize use of the shared baos */
	final java.io.ByteArrayOutputStream baos = new ByteArrayOutputStream();
	final java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(
		baos);

	oos.writeObject(o);
	oos.flush();

	return baos.toByteArray();
    }

    private static Object fromByteArray(final byte[] buf) {
	if (buf == null) {
	    return null;
	}

	try {
	    final java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(
		    buf);
	    final java.io.ObjectInputStream ois = new java.io.ObjectInputStream(
		    bais);
	    return ois.readObject();
	} catch (final java.lang.ClassNotFoundException e) {
	} catch (final java.io.IOException e) {
	}

	return null;
    }

    /**
     * Get the byte array from a bitstr, padded with zero bits in the little end
     * of the last byte.
     * 
     * @return the byte array containing the bytes for this bitstr.
     */
    public byte[] binaryValue() {
	return bin;
    }

    /**
     * Get the size in whole bytes of the bitstr, rest bits in the last byte not
     * counted.
     * 
     * @return the number of bytes contained in the bintstr.
     */
    public int size() {
	if (pad_bits == 0) {
	    return bin.length;
	}
	if (bin.length == 0) {
	    throw new java.lang.IllegalStateException("Impossible length");
	}
	return bin.length - 1;
    }

    /**
     * Get the number of pad bits in the last byte of the bitstr. The pad bits
     * are zero and in the little end.
     * 
     * @return the number of pad bits in the bitstr.
     */
    public int pad_bits() {
	return pad_bits;
    }

    /**
     * Get the java Object from the bitstr. If the bitstr contains a serialized
     * Java object, then this method will recreate the object.
     * 
     * 
     * @return the java Object represented by this bitstr, or null if the bitstr
     *         does not represent a Java Object.
     */
    public Object getObject() {
	if (pad_bits != 0) {
	    return null;
	}
	return fromByteArray(bin);
    }

    /**
     * Get the string representation of this bitstr object. A bitstr is printed
     * as #Bin&lt;N&gt;, where N is the number of bytes contained in the object
     * or #bin&lt;N-M&gt; if there are M pad bits.
     * 
     * @return the Erlang string representation of this bitstr.
     */
    @Override
    public String toString() {
	if (pad_bits == 0) {
	    return "#Bin<" + bin.length + ">";
	}
	if (bin.length == 0) {
	    throw new java.lang.IllegalStateException("Impossible length");
	}
	return "#Bin<" + bin.length + "-" + pad_bits + ">";
    }

    /**
     * Convert this bitstr to the equivalent Erlang external representation.
     * 
     * @param buf
     *                an output stream to which the encoded bitstr should be
     *                written.
     */
    @Override
    public void encode(final OtpOutputStream buf) {
	buf.write_bitstr(bin, pad_bits);
    }

    /**
     * Determine if two bitstrs are equal. Bitstrs are equal if they have the
     * same byte length and tail length, and the array of bytes is identical.
     * 
     * @param o
     *                the bitstr to compare to.
     * 
     * @return true if the bitstrs contain the same bits, false otherwise.
     */
    @Override
    public boolean equals(final Object o) {
	if (!(o instanceof OtpErlangBitstr)) {
	    return false;
	}

	final OtpErlangBitstr that = (OtpErlangBitstr) o;
	if (pad_bits != that.pad_bits) {
	    return false;
	}

	final int len = bin.length;
	if (len != that.bin.length) {
	    return false;
	}

	for (int i = 0; i < len; i++) {
	    if (bin[i] != that.bin[i]) {
		return false; // early exit
	    }
	}

	return true;
    }
    
    @Override
    protected int doHashCode() {
	OtpErlangObject.Hash hash = new OtpErlangObject.Hash(15);
	hash.combine(bin);
	hash.combine(pad_bits);
	return hash.valueOf();
    }
    
    @Override
    public Object clone() {
	final OtpErlangBitstr that = (OtpErlangBitstr) super.clone();
	that.bin = bin.clone();
	that.pad_bits = pad_bits;
	return that;
    }
}