Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.amqp;
22
23 import java.nio.ByteBuffer;
24 import java.util.Collection;
25
26 import org.apache.qpid.proton.codec.ReadableBuffer;
27
28 public final class Binary
29 {
30 private final byte[] _data;
31 private final int _offset;
32 private final int _length;
33 private int _hashCode;
34
35 public Binary(final byte[] data)
36 {
37 this(data, 0, data.length);
38 }
39
40 public Binary(final byte[] data, final int offset, final int length)
41 {
42 _data = data;
43 _offset = offset;
44 _length = length;
45 }
46
47 public ByteBuffer asByteBuffer()
48 {
49 return ByteBuffer.wrap(_data, _offset, _length);
50 }
51
52 @Override
53 public final int hashCode()
54 {
55 int hc = _hashCode;
56 if(hc == 0)
57 {
58 for (int i = 0; i < _length; i++)
59 {
60 hc = 31*hc + (0xFF & _data[_offset + i]);
61 }
62 _hashCode = hc;
63 }
64 return hc;
65 }
66
67 @Override
68 public final boolean equals(Object o)
69 {
70 if (this == o)
71 {
72 return true;
73 }
74
75 if (o == null || getClass() != o.getClass())
76 {
77 return false;
78 }
79
80 Binary buf = (Binary) o;
81 final int size = _length;
82 if (size != buf._length)
83 {
84 return false;
85 }
86
87 final byte[] myData = _data;
88 final byte[] theirData = buf._data;
89 int myOffset = _offset;
90 int theirOffset = buf._offset;
91 final int myLimit = myOffset + size;
92
93 while(myOffset < myLimit)
94 {
95 if (myData[myOffset++] != theirData[theirOffset++])
96 {
97 return false;
98 }
99 }
100
101 return true;
102 }
103
104 public int getArrayOffset()
105 {
106 return _offset;
107 }
108
109 public byte[] getArray()
110 {
111 return _data;
112 }
113
114 public int getLength()
115 {
116 return _length;
117 }
118
119 @Override
120 public String toString()
121 {
122 StringBuilder str = new StringBuilder();
123
124 for (int i = 0; i < _length; i++)
125 {
126 byte c = _data[_offset + i];
127
128 if (c > 31 && c < 127 && c != '\\')
129 {
130 str.append((char)c);
131 }
132 else
133 {
134 str.append(String.format("\\x%02x", c));
135 }
136 }
137
138 return str.toString();
139 }
140
141 public static Binary combine(final Collection<Binary> binaries)
142 {
143 if(binaries.size() == 1)
144 {
145 return binaries.iterator().next();
146 }
147
148 int size = 0;
149 for(Binary binary : binaries)
150 {
151 size += binary.getLength();
152 }
153 byte[] data = new byte[size];
154 int offset = 0;
155 for(Binary binary : binaries)
156 {
157 System.arraycopy(binary._data, binary._offset, data, offset, binary._length);
158 offset += binary._length;
159 }
160 return new Binary(data);
161 }
162
163 public Binary subBinary(final int offset, final int length)
164 {
165 return new Binary(_data, _offset+offset, length);
166 }
167
168 public static Binary create(ReadableBuffer buffer)
169 {
170 if (buffer == null)
171 {
172 return null;
173 }
174 else if (!buffer.hasArray())
175 {
176 byte data[] = new byte [buffer.remaining()];
177 ReadableBuffer dup = buffer.duplicate();
178 dup.get(data);
179 return new Binary(data);
180 }
181 else
182 {
183 return new Binary(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
184 }
185 }
186
187 public static Binary create(ByteBuffer buffer)
188 {
189 if (buffer == null)
190 {
191 return null;
192 }
193 if (buffer.isDirect() || buffer.isReadOnly())
194 {
195 byte data[] = new byte [buffer.remaining()];
196 ByteBuffer dup = buffer.duplicate();
197 dup.get(data);
198 return new Binary(data);
199 }
200 else
201 {
202 return new Binary(buffer.array(), buffer.arrayOffset()+buffer.position(), buffer.remaining());
203 }
204 }
205
206 public static Binary copy(Binary source)
207 {
208 if (source == null)
209 {
210 return null;
211 }
212 else
213 {
214 byte[] data = new byte[source.getLength()];
215 System.arraycopy(source.getArray(), source.getArrayOffset(), data, 0, source.getLength());
216 return new Binary(data);
217 }
218 }
219 }