1
/* ``The contents of this file are subject to the Erlang Public License,
4
* Copyright Ericsson AB 2000-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
2
7
* Version 1.1, (the "License"); you may not use this file except in
3
8
* compliance with the License. You should have received a copy of the
4
9
* Erlang Public License along with this software. If not, it can be
5
* retrieved via the world wide web at http://www.erlang.org/.
10
* retrieved online at http://www.erlang.org/.
7
12
* Software distributed under the License is distributed on an "AS IS"
8
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
14
* the License for the specific language governing rights and limitations
10
15
* under the License.
12
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
* AB. All Rights Reserved.''
18
19
package com.ericsson.otp.erlang;
20
21
import java.io.Serializable;
21
import java.io.IOException;
22
import java.io.ByteArrayInputStream;
23
import java.io.ByteArrayOutputStream;
24
import java.io.ObjectInputStream;
25
import java.io.ObjectOutputStream;
28
* Provides a Java representation of Erlang binaries. Anything that
29
* can be represented as a sequence of bytes can be made into an
32
public class OtpErlangBinary extends OtpErlangBitstr
33
implements Serializable, Cloneable
24
* Provides a Java representation of Erlang binaries. Anything that can be
25
* represented as a sequence of bytes can be made into an Erlang binary.
27
public class OtpErlangBinary extends OtpErlangBitstr implements Serializable,
35
29
// don't change this!
36
30
static final long serialVersionUID = -3781009633593609217L;
39
33
* Create a binary from a byte array
41
* @param bin the array of bytes from which to create the binary.
43
public OtpErlangBinary(byte[] bin) {
36
* the array of bytes from which to create the binary.
38
public OtpErlangBinary(final byte[] bin) {
48
* Create a binary from a stream containing a binary encoded in
49
* Erlang external format.
51
* @param buf the stream containing the encoded binary.
53
* @exception OtpErlangDecodeException if the buffer does not
54
* contain a valid external representation of an Erlang binary.
56
public OtpErlangBinary(OtpInputStream buf) throws OtpErlangDecodeException
43
* Create a binary from a stream containing a binary encoded in Erlang
47
* the stream containing the encoded binary.
49
* @exception OtpErlangDecodeException
50
* if the buffer does not contain a valid external
51
* representation of an Erlang binary.
53
public OtpErlangBinary(final OtpInputStream buf)
54
throws OtpErlangDecodeException {
58
55
super(new byte[0]);
59
this.bin = buf.read_binary();
56
bin = buf.read_binary();
64
* Create a binary from an arbitrary Java Object. The object must
65
* implement java.io.Serializable or java.io.Externalizable.
67
* @param o the object to serialize and create this binary from.
69
public OtpErlangBinary(Object o) {
61
* Create a binary from an arbitrary Java Object. The object must implement
62
* java.io.Serializable or java.io.Externalizable.
65
* the object to serialize and create this binary from.
67
public OtpErlangBinary(final Object o) {
76
72
* Convert this binary to the equivalent Erlang external representation.
78
* @param buf an output stream to which the encoded binary should be
81
public void encode(OtpOutputStream buf) {
82
buf.write_binary(this.bin);
86
* Determine if two binaries are equal. Binaries are equal if they have
87
* the same length and the array of bytes is identical.
89
* @param o the binary to compare to.
91
* @return true if the byte arrays contain the same bytes, false
94
public boolean equals(Object o) {
95
if (! (o instanceof OtpErlangBinary)) return false;
97
OtpErlangBinary that = (OtpErlangBinary)o;
98
int len = this.bin.length;
99
if (len != that.bin.length) return false;
101
for (int i = 0; i < len; i++) {
102
if (this.bin[i] != that.bin[i]) return false; // early exit
75
* an output stream to which the encoded binary should be
79
public void encode(final OtpOutputStream buf) {
80
buf.write_binary(bin);
108
84
public Object clone() {
109
OtpErlangBinary that = (OtpErlangBinary)(super.clone());
85
final OtpErlangBinary that = (OtpErlangBinary) super.clone();