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 boolean equals(final Object obj) {
032        if (obj == null) {
033            return false;
034        }
035        if (!(obj instanceof BinaryConstant)) {
036            return false;
037        }
038        final BinaryConstant other = (BinaryConstant) obj;
039        return equals(other.value);
040    }
041
042    public boolean equals(final byte[] bytes) {
043        return Arrays.equals(value, bytes);
044    }
045
046    public boolean equals(final byte[] bytes, final int offset, final int length) {
047        if (value.length != length) {
048            return false;
049        }
050        for (int i = 0; i < length; i++) {
051            if (value[i] != bytes[offset + i]) {
052                return false;
053            }
054        }
055        return true;
056    }
057
058    @Override
059    public int hashCode() {
060        return Arrays.hashCode(value);
061    }
062
063    public byte get(final int i) {
064        return value[i];
065    }
066
067    public int size() {
068        return value.length;
069    }
070
071    public byte[] toByteArray() {
072        return value.clone();
073    }
074
075    public void writeTo(final OutputStream os) throws IOException {
076        for (final byte element : value) {
077            os.write(element);
078        }
079    }
080}