~ubuntu-branches/ubuntu/maverick/protobuf/maverick

« back to all changes in this revision

Viewing changes to java/src/main/java/com/google/protobuf/ByteString.java

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
public final class ByteString {
47
47
  private final byte[] bytes;
48
48
 
49
 
  private ByteString(byte[] bytes) {
 
49
  private ByteString(final byte[] bytes) {
50
50
    this.bytes = bytes;
51
51
  }
52
52
 
55
55
   *
56
56
   * @throws ArrayIndexOutOfBoundsException {@code index} is < 0 or >= size
57
57
   */
58
 
  public byte byteAt(int index) {
 
58
  public byte byteAt(final int index) {
59
59
    return bytes[index];
60
60
  }
61
61
 
63
63
   * Gets the number of bytes.
64
64
   */
65
65
  public int size() {
66
 
    return this.bytes.length;
 
66
    return bytes.length;
67
67
  }
68
68
 
69
69
  /**
70
70
   * Returns {@code true} if the size is {@code 0}, {@code false} otherwise.
71
71
   */
72
72
  public boolean isEmpty() {
73
 
    return this.bytes.length == 0;
 
73
    return bytes.length == 0;
74
74
  }
75
75
 
76
76
  // =================================================================
84
84
  /**
85
85
   * Copies the given bytes into a {@code ByteString}.
86
86
   */
87
 
  public static ByteString copyFrom(byte[] bytes, int offset, int size) {
88
 
    byte[] copy = new byte[size];
 
87
  public static ByteString copyFrom(final byte[] bytes, final int offset,
 
88
                                    final int size) {
 
89
    final byte[] copy = new byte[size];
89
90
    System.arraycopy(bytes, offset, copy, 0, size);
90
91
    return new ByteString(copy);
91
92
  }
93
94
  /**
94
95
   * Copies the given bytes into a {@code ByteString}.
95
96
   */
96
 
  public static ByteString copyFrom(byte[] bytes) {
 
97
  public static ByteString copyFrom(final byte[] bytes) {
97
98
    return copyFrom(bytes, 0, bytes.length);
98
99
  }
99
100
 
101
102
   * Encodes {@code text} into a sequence of bytes using the named charset
102
103
   * and returns the result as a {@code ByteString}.
103
104
   */
104
 
  public static ByteString copyFrom(String text, String charsetName)
 
105
  public static ByteString copyFrom(final String text, final String charsetName)
105
106
      throws UnsupportedEncodingException {
106
107
    return new ByteString(text.getBytes(charsetName));
107
108
  }
110
111
   * Encodes {@code text} into a sequence of UTF-8 bytes and returns the
111
112
   * result as a {@code ByteString}.
112
113
   */
113
 
  public static ByteString copyFromUtf8(String text) {
 
114
  public static ByteString copyFromUtf8(final String text) {
114
115
    try {
115
116
      return new ByteString(text.getBytes("UTF-8"));
116
117
    } catch (UnsupportedEncodingException e) {
127
128
   * @param target buffer to copy into
128
129
   * @param offset in the target buffer
129
130
   */
130
 
  public void copyTo(byte[] target, int offset) {
 
131
  public void copyTo(final byte[] target, final int offset) {
131
132
    System.arraycopy(bytes, 0, target, offset, bytes.length);
132
133
  }
133
134
 
139
140
   * @param targetOffset offset within the target buffer
140
141
   * @param size number of bytes to copy
141
142
   */
142
 
  public void copyTo(byte[] target, int sourceOffset, int targetOffset,
143
 
      int size) {
 
143
  public void copyTo(final byte[] target, final int sourceOffset,
 
144
                     final int targetOffset,
 
145
      final int size) {
144
146
    System.arraycopy(bytes, sourceOffset, target, targetOffset, size);
145
147
  }
146
148
 
148
150
   * Copies bytes to a {@code byte[]}.
149
151
   */
150
152
  public byte[] toByteArray() {
151
 
    int size = this.bytes.length;
152
 
    byte[] copy = new byte[size];
153
 
    System.arraycopy(this.bytes, 0, copy, 0, size);
 
153
    final int size = bytes.length;
 
154
    final byte[] copy = new byte[size];
 
155
    System.arraycopy(bytes, 0, copy, 0, size);
154
156
    return copy;
155
157
  }
156
158
 
159
161
   * same backing byte array.
160
162
   */
161
163
  public ByteBuffer asReadOnlyByteBuffer() {
162
 
    ByteBuffer byteBuffer = ByteBuffer.wrap(this.bytes);
 
164
    final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
163
165
    return byteBuffer.asReadOnlyBuffer();
164
166
  }
165
167
 
167
169
   * Constructs a new {@code String} by decoding the bytes using the
168
170
   * specified charset.
169
171
   */
170
 
  public String toString(String charsetName)
 
172
  public String toString(final String charsetName)
171
173
      throws UnsupportedEncodingException {
172
 
    return new String(this.bytes, charsetName);
 
174
    return new String(bytes, charsetName);
173
175
  }
174
176
 
175
177
  /**
177
179
   */
178
180
  public String toStringUtf8() {
179
181
    try {
180
 
      return new String(this.bytes, "UTF-8");
 
182
      return new String(bytes, "UTF-8");
181
183
    } catch (UnsupportedEncodingException e) {
182
184
      throw new RuntimeException("UTF-8 not supported?", e);
183
185
    }
187
189
  // equals() and hashCode()
188
190
 
189
191
  @Override
190
 
  public boolean equals(Object o) {
 
192
  public boolean equals(final Object o) {
191
193
    if (o == this) {
192
194
      return true;
193
195
    }
196
198
      return false;
197
199
    }
198
200
 
199
 
    ByteString other = (ByteString) o;
200
 
    int size = this.bytes.length;
 
201
    final ByteString other = (ByteString) o;
 
202
    final int size = bytes.length;
201
203
    if (size != other.bytes.length) {
202
204
      return false;
203
205
    }
204
206
 
205
 
    byte[] bytes = this.bytes;
206
 
    byte[] otherBytes = other.bytes;
 
207
    final byte[] thisBytes = bytes;
 
208
    final byte[] otherBytes = other.bytes;
207
209
    for (int i = 0; i < size; i++) {
208
 
      if (bytes[i] != otherBytes[i]) {
 
210
      if (thisBytes[i] != otherBytes[i]) {
209
211
        return false;
210
212
      }
211
213
    }
213
215
    return true;
214
216
  }
215
217
 
216
 
  volatile int hash = 0;
 
218
  private volatile int hash = 0;
217
219
 
218
220
  @Override
219
221
  public int hashCode() {
220
 
    int h = this.hash;
 
222
    int h = hash;
221
223
 
222
224
    if (h == 0) {
223
 
      byte[] bytes = this.bytes;
224
 
      int size = this.bytes.length;
 
225
      final byte[] thisBytes = bytes;
 
226
      final int size = bytes.length;
225
227
 
226
228
      h = size;
227
229
      for (int i = 0; i < size; i++) {
228
 
        h = h * 31 + bytes[i];
 
230
        h = h * 31 + thisBytes[i];
229
231
      }
230
232
      if (h == 0) {
231
233
        h = 1;
232
234
      }
233
235
 
234
 
      this.hash = h;
 
236
      hash = h;
235
237
    }
236
238
 
237
239
    return h;
264
266
  /**
265
267
   * Creates a new {@link Output} with the given initial capacity.
266
268
   */
267
 
  public static Output newOutput(int initialCapacity) {
 
269
  public static Output newOutput(final int initialCapacity) {
268
270
    return new Output(new ByteArrayOutputStream(initialCapacity));
269
271
  }
270
272
 
285
287
    /**
286
288
     * Constructs a new output with the given initial capacity.
287
289
     */
288
 
    private Output(ByteArrayOutputStream bout) {
 
290
    private Output(final ByteArrayOutputStream bout) {
289
291
      super(bout);
290
292
      this.bout = bout;
291
293
    }
294
296
     * Creates a {@code ByteString} instance from this {@code Output}.
295
297
     */
296
298
    public ByteString toByteString() {
297
 
      byte[] byteArray = bout.toByteArray();
 
299
      final byte[] byteArray = bout.toByteArray();
298
300
      return new ByteString(byteArray);
299
301
    }
300
302
  }
301
303
 
302
304
  /**
303
305
   * Constructs a new ByteString builder, which allows you to efficiently
304
 
   * construct a {@code ByteString} by writing to a {@link CodedOutputSteam}.
 
306
   * construct a {@code ByteString} by writing to a {@link CodedOutputStream}.
305
307
   * Using this is much more efficient than calling {@code newOutput()} and
306
308
   * wrapping that in a {@code CodedOutputStream}.
307
309
   *
312
314
   * @param size The target byte size of the {@code ByteString}.  You must
313
315
   *             write exactly this many bytes before building the result.
314
316
   */
315
 
  static CodedBuilder newCodedBuilder(int size) {
 
317
  static CodedBuilder newCodedBuilder(final int size) {
316
318
    return new CodedBuilder(size);
317
319
  }
318
320
 
321
323
    private final CodedOutputStream output;
322
324
    private final byte[] buffer;
323
325
 
324
 
    private CodedBuilder(int size) {
 
326
    private CodedBuilder(final int size) {
325
327
      buffer = new byte[size];
326
328
      output = CodedOutputStream.newInstance(buffer);
327
329
    }