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 public boolean equals(final byte[] bytes) { 031 return Arrays.equals(value, bytes); 032 } 033 034 @Override 035 public boolean equals(final Object obj) { 036 if (obj == null) { 037 return false; 038 } 039 if (!(obj instanceof BinaryConstant)) { 040 return false; 041 } 042 final BinaryConstant other = (BinaryConstant) obj; 043 return equals(other.value); 044 } 045 046 public byte get(final int i) { 047 return value[i]; 048 } 049 050 @Override 051 public int hashCode() { 052 return Arrays.hashCode(value); 053 } 054 055 /** 056 * Tests whether the give buffer starts with this binary constant. 057 * 058 * @param buffer The buffer to test. 059 * @return Whether the give buffer starts with this binary constant. 060 */ 061 public boolean isStartOf(final byte[] buffer) { 062 return BinaryFunctions.startsWith(buffer, value); 063 } 064 065 byte[] rawValue() { 066 return value; 067 } 068 069 public int size() { 070 return value.length; 071 } 072 073 public void writeTo(final OutputStream os) throws IOException { 074 for (final byte element : value) { 075 os.write(element); 076 } 077 } 078}