001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.common; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.util.Arrays; 022 023public class BinaryConstant { 024 private final byte[] value; 025 026 public BinaryConstant(final byte[] value) { 027 this.value = value.clone(); 028 } 029 030 @Override 031 public BinaryConstant clone() throws CloneNotSupportedException { 032 return (BinaryConstant) super.clone(); 033 } 034 035 @Override 036 public boolean equals(final Object obj) { 037 if (obj == null) { 038 return false; 039 } 040 if (!(obj instanceof BinaryConstant)) { 041 return false; 042 } 043 final BinaryConstant other = (BinaryConstant) obj; 044 return equals(other.value); 045 } 046 047 public boolean equals(final byte[] bytes) { 048 return Arrays.equals(value, bytes); 049 } 050 051 public boolean equals(final byte[] bytes, final int offset, final int length) { 052 if (value.length != length) { 053 return false; 054 } 055 for (int i = 0; i < length; i++) { 056 if (value[i] != bytes[offset + i]) { 057 return false; 058 } 059 } 060 return true; 061 } 062 063 @Override 064 public int hashCode() { 065 return Arrays.hashCode(value); 066 } 067 068 public byte get(final int i) { 069 return value[i]; 070 } 071 072 public int size() { 073 return value.length; 074 } 075 076 public byte[] toByteArray() { 077 return value.clone(); 078 } 079 080 public void writeTo(final OutputStream os) throws IOException { 081 for (final byte element : value) { 082 os.write(element); 083 } 084 } 085}