/bson/src/main/org/bson/types/Binary.java

http://github.com/mongodb/mongo-java-driver · Java · 117 lines · 51 code · 16 blank · 50 comment · 9 complexity · 70e13cc056f334e675f68eece9c694e5 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.bson.types;
  17. import org.bson.BsonBinarySubType;
  18. import java.io.Serializable;
  19. import java.util.Arrays;
  20. /**
  21. * Generic binary holder.
  22. */
  23. public class Binary implements Serializable {
  24. private static final long serialVersionUID = 7902997490338209467L;
  25. private final byte type;
  26. private final byte[] data;
  27. /**
  28. * Creates a Binary object with the default binary type of 0
  29. *
  30. * @param data raw data
  31. */
  32. public Binary(final byte[] data) {
  33. this(BsonBinarySubType.BINARY, data);
  34. }
  35. /**
  36. * Creates a Binary with the specified type and data.
  37. *
  38. * @param type the binary type
  39. * @param data the binary data
  40. */
  41. public Binary(final BsonBinarySubType type, final byte[] data) {
  42. this(type.getValue(), data);
  43. }
  44. /**
  45. * Creates a Binary object
  46. *
  47. * @param type type of the field as encoded in BSON
  48. * @param data raw data
  49. */
  50. public Binary(final byte type, final byte[] data) {
  51. this.type = type;
  52. this.data = data.clone();
  53. }
  54. /**
  55. * Get the binary sub type as a byte.
  56. *
  57. * @return the binary sub type as a byte.
  58. */
  59. public byte getType() {
  60. return type;
  61. }
  62. /**
  63. * Get a copy of the binary value.
  64. *
  65. * @return a copy of the binary value.
  66. */
  67. public byte[] getData() {
  68. return data.clone();
  69. }
  70. /**
  71. * Get the length of the data.
  72. *
  73. * @return the length of the binary array.
  74. */
  75. public int length() {
  76. return data.length;
  77. }
  78. @Override
  79. public boolean equals(final Object o) {
  80. if (this == o) {
  81. return true;
  82. }
  83. if (o == null || getClass() != o.getClass()) {
  84. return false;
  85. }
  86. Binary binary = (Binary) o;
  87. if (type != binary.type) {
  88. return false;
  89. }
  90. if (!Arrays.equals(data, binary.data)) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. @Override
  96. public int hashCode() {
  97. int result = (int) type;
  98. result = 31 * result + Arrays.hashCode(data);
  99. return result;
  100. }
  101. }