01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.x.imageio.plugins;
18:
19: import java.io.IOException;
20: import java.util.Arrays;
21:
22: import javax.imageio.stream.ImageInputStream;
23:
24: import org.apache.harmony.x.imageio.internal.nls.Messages;
25:
26: public enum ImageSignature {
27: JPEG(new byte[] { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF }), BMP(
28: new byte[] { 'B', 'M' }), GIF87a(new byte[] { 'G', 'I',
29: 'F', '8', '7', 'a' }), GIF89a(new byte[] { 'G', 'I', 'F',
30: '8', '9', 'a' }), PNG(new byte[] { (byte) 0x89,
31: (byte) 0x50, (byte) 0x4E, (byte) 0x47, (byte) 0x0D,
32: (byte) 0x0A, (byte) 0x1A, (byte) 0x0A });
33:
34: private final byte[] sig;
35:
36: ImageSignature(final byte[] sig) {
37: this .sig = sig;
38: }
39:
40: public static byte[] readSignature(final Object source,
41: final int len) throws IOException {
42: if (source == null) {
43: throw new IllegalArgumentException(Messages.getString(
44: "imageio.2", //$NON-NLS-1$
45: "source")); //$NON-NLS-1$
46: }
47:
48: if (!(source instanceof ImageInputStream)) {
49: return null;
50: }
51:
52: final ImageInputStream iis = (ImageInputStream) source;
53: final byte[] sig = new byte[len];
54:
55: iis.mark();
56: iis.readFully(sig);
57: iis.reset();
58:
59: return sig;
60: }
61:
62: public byte[] getBytes() {
63: return sig.clone();
64: }
65:
66: public boolean verify(final byte[] sig) {
67: return Arrays.equals(this .sig, sig);
68: }
69:
70: public boolean verify(final Object source) throws IOException {
71: return verify(readSignature(source, sig.length));
72: }
73: }
|