git.iko.soy/opencode-android
on master help sign in
54 lines 1.4 kB history raw
1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
4package com.novi.serde;
6import java.util.Arrays;
7import java.util.Objects;
9/**
10 * Immutable wrapper class around byte[].
11 *
12 * Enforces value-semantice for `equals` and `hashCode`.
13 */
14public final class Bytes {
15 private final byte[] content;
17 private static final Bytes EMPTY = new Bytes(new byte[0]);
19 /// Low-level constructor (use with care).
20 public Bytes(byte[] content) {
21 Objects.requireNonNull(content, "content must not be null");
22 this.content = content;
23 }
25 public static Bytes empty() {
26 return EMPTY;
27 }
29 public static Bytes valueOf(byte[] content) {
30 Objects.requireNonNull(content, "content must not be null");
31 if (content.length == 0) {
32 return EMPTY;
33 } else {
34 return new Bytes(content.clone());
35 }
36 }
38 public byte[] content() {
39 return this.content.clone();
40 }
42 public boolean equals(Object obj) {
43 if (this == obj) return true;
44 if (obj == null) return false;
45 if (getClass() != obj.getClass()) return false;
46 Bytes other = (Bytes) obj;
47 return Arrays.equals(this.content, other.content);
48 }
50 public int hashCode() {
51 return Arrays.hashCode(content);
52 }