All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.net.NetByteArray Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.net;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;

import javax.xml.bind.DatatypeConverter;

/**
 * @author [email protected] (Marcio Endo)
 */
public class NetByteArray implements BufferWritable {

  private final byte[] value;

  private NetByteArray(byte[] value) {
    this.value = value;
  }

  public static NetByteArray read(ByteBuffer buffer, int length) {
    byte[] value = new byte[length];
    buffer.get(value);
    return new NetByteArray(value);
  }

  public static NetByteArray wrap(byte[] bytes) {
    Objects.requireNonNull(bytes);
    return new NetByteArray(bytes);
  }

  public byte[] byteArray() {
    return value;
  }

  @Override
  public final boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof NetByteArray)) {
      return false;
    }
    NetByteArray that = (NetByteArray) obj;
    return Arrays.equals(value, that.value);
  }

  @Override
  public final int hashCode() {
    return Arrays.hashCode(value);
  }

  @Override
  public int length() {
    return value.length;
  }

  @Override
  public String toString() {
    return DatatypeConverter.printHexBinary(value);
  }

  @Override
  public void writeTo(Buffer buffer) {
    buffer.write(value);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy