|
1 /* |
|
2 * Copyright (c) 2010 Remko Tronçon |
|
3 * Licensed under the GNU General Public License v3. |
|
4 * See Documentation/Licenses/GPLv3.txt for more information. |
|
5 */ |
|
6 /* |
|
7 * Copyright (c) 2010, Isode Limited, London, England. |
|
8 * All rights reserved. |
|
9 */ |
|
10 package com.isode.stroke.base; |
|
11 |
|
12 import java.io.UnsupportedEncodingException; |
|
13 |
|
14 /** |
|
15 * |
|
16 */ |
|
17 public class ByteArray { |
|
18 |
|
19 public ByteArray() { |
|
20 } |
|
21 |
|
22 public ByteArray(String s) { |
|
23 try { |
|
24 fromBytes(s.getBytes("UTF-8")); |
|
25 } catch (UnsupportedEncodingException ex) { |
|
26 throw new IllegalStateException("JVM has no 'UTF-8' encoding"); |
|
27 } |
|
28 } |
|
29 |
|
30 public ByteArray(byte[] c) { |
|
31 fromBytes(c); |
|
32 } |
|
33 |
|
34 public ByteArray(ByteArray b) { |
|
35 fromBytes(b.getData()); |
|
36 } |
|
37 |
|
38 private void fromBytes(final byte[] b) { |
|
39 data_ = new byte[b.length]; |
|
40 System.arraycopy(b, 0, data_, 0, b.length); |
|
41 } |
|
42 |
|
43 /*public ByteArray(char[] c, int n) { |
|
44 for (int i = 0; i < n; i++) { |
|
45 append(c[i]); |
|
46 } |
|
47 }*/ |
|
48 |
|
49 /** |
|
50 * These are the raw, modifyable data! |
|
51 * @return |
|
52 */ |
|
53 public byte[] getData() { |
|
54 return data_; |
|
55 } |
|
56 |
|
57 public int getSize() { |
|
58 return data_.length; |
|
59 } |
|
60 |
|
61 public boolean isEmpty() { |
|
62 return getSize() == 0; |
|
63 } |
|
64 |
|
65 /*public void resize(size_t size) { |
|
66 return data_.resize(size); |
|
67 }*/ |
|
68 /** Immutable add */ |
|
69 public static ByteArray plus(ByteArray a, ByteArray b) { |
|
70 ByteArray x = new ByteArray(a.getData()); |
|
71 x.append(b); |
|
72 return x; |
|
73 } |
|
74 |
|
75 /** Immutable add */ |
|
76 /*public ByteArray plus(ByteArray a, char b) { |
|
77 ByteArray x = new ByteArray(a.getData()); |
|
78 x.append(b); |
|
79 return x; |
|
80 }*/ |
|
81 |
|
82 /** Mutable add */ |
|
83 public ByteArray append(ByteArray b) { |
|
84 append(b.getData()); |
|
85 return this; |
|
86 } |
|
87 |
|
88 /** Mutable add */ |
|
89 private ByteArray append(byte[] b) { |
|
90 int newLength = data_.length + b.length; |
|
91 byte[] newData = new byte[newLength]; |
|
92 for (int i = 0; i < data_.length; i++) { |
|
93 newData[i] = data_[i]; |
|
94 } |
|
95 for (int i = 0; i < b.length; i++) { |
|
96 newData[i + data_.length] = b[i]; |
|
97 } |
|
98 data_ = newData; |
|
99 return this; |
|
100 } |
|
101 |
|
102 /** Mutable add */ |
|
103 public ByteArray append(byte b) { |
|
104 byte[] bytes = {b}; |
|
105 append(bytes); |
|
106 return this; |
|
107 } |
|
108 |
|
109 /** mutable add */ |
|
110 public ByteArray append(String s) { |
|
111 byte[] bytes; |
|
112 try { |
|
113 bytes = s.getBytes("UTF-8"); |
|
114 } catch (UnsupportedEncodingException ex) { |
|
115 throw new IllegalStateException("JVM has no 'UTF-8' encoding"); |
|
116 } |
|
117 append(bytes); |
|
118 return this; |
|
119 } |
|
120 |
|
121 @Override |
|
122 public int hashCode() { |
|
123 int hash = 3; |
|
124 hash = 97 * hash + (this.data_ != null ? this.data_.hashCode() : 0); |
|
125 return hash; |
|
126 } |
|
127 |
|
128 @Override |
|
129 public boolean equals(Object other) { |
|
130 return other instanceof ByteArray && toString().equals(other.toString()); |
|
131 } |
|
132 |
|
133 /*public char charAt(int i) { |
|
134 return data_.charAt(i); |
|
135 }*/ |
|
136 |
|
137 /*public const_iterator begin() const { |
|
138 return data_.begin(); |
|
139 } |
|
140 |
|
141 public const_iterator end() const { |
|
142 return data_.end(); |
|
143 }*/ |
|
144 @Override |
|
145 public String toString() { |
|
146 try { |
|
147 return new String(data_, "UTF-8"); |
|
148 } catch (UnsupportedEncodingException ex) { |
|
149 throw new IllegalStateException("JVM has no 'UTF-8' encoding"); |
|
150 } |
|
151 } |
|
152 |
|
153 public void readFromFile(String file) { |
|
154 //FIXME: port |
|
155 } |
|
156 |
|
157 public void clear() { |
|
158 data_ = new byte[]{}; |
|
159 } |
|
160 private byte[] data_ = {}; |
|
161 |
|
162 } |