org.apache.batik.ext.awt.image.spi.MagicNumberRegistryEntry Maven / Gradle / Ivy
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 org.apache.batik.ext.awt.image.spi;
import java.io.IOException;
import java.io.InputStream;
import java.io.StreamCorruptedException;
/**
* This Image tag registry entry is built around the notion of magic
* numbers. These are strings of bytes that are at a well known
* location in the input stream (often the start).
*
* This base class can handle the compatibility check based on a list
* of Magic Numbers that correspond to your format (Some formats have
* multiple magic numbers associated with them).
*
* @version $Id: MagicNumberRegistryEntry.java 1804130 2017-08-04 14:41:11Z ssteiner $
*/
public abstract class MagicNumberRegistryEntry
extends AbstractRegistryEntry
implements StreamRegistryEntry {
public static final float PRIORITY = 1000;
/**
* Inner class that represents one magic number. Simply consists
* of an offset in bytes from the start of the file, and a byte
* array that must match.
*/
public static class MagicNumber {
int offset;
byte [] magicNumber;
byte [] buffer;
/**
* Constructor.
* @param offset the location of the magic number in file.
* @param magicNumber the sequence of bytes that must match.
*/
public MagicNumber(int offset, byte[]magicNumber) {
this.offset = offset;
this.magicNumber = magicNumber.clone();
buffer = new byte[magicNumber.length];
}
/**
* Returns the maximum number of bytes that will be read for
* this magic number comparison.
*/
int getReadlimit() {
return offset+magicNumber.length;
}
/**
* Performs the check of is.
*/
boolean isMatch(InputStream is)
throws StreamCorruptedException {
int idx = 0;
is.mark(getReadlimit());
try {
// Skip to the offset location.
while (idx < offset) {
int rn = (int)is.skip(offset-idx);
if (rn == -1) {
return false;
}
idx += rn;
}
idx = 0;
while (idx < buffer.length) {
int rn = is.read(buffer, idx, buffer.length-idx);
if (rn == -1) {
return false;
}
idx += rn;
}
for (int i=0; i maxbuf) {
maxbuf = req;
}
}
return maxbuf;
}
/**
* Check if the stream contains an image that can be
* handled by this format handler
*/
public boolean isCompatibleStream(InputStream is)
throws StreamCorruptedException {
for (MagicNumber magicNumber : magicNumbers) {
if (magicNumber.isMatch(is)) {
return true;
}
}
return false;
}
}