Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / gnu / javax / imageio / bmp / BMPFileHeader.java
blob246e0eacfeb7c9abf0c1569eb1021368b8ae16c4
1 /* BMPFileHeader.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.javax.imageio.bmp;
40 import java.io.IOException;
41 import javax.imageio.stream.ImageInputStream;
42 import java.io.OutputStream;
43 import java.nio.ByteBuffer;
44 import java.nio.ByteOrder;
46 public class BMPFileHeader {
47 /** Header signature, always 'BM' */
48 private final static short bfType = 0x424d;
50 /** Bitmap file size, in bytes. */
51 private long bfSize;
53 /** Offset from the beginning of the file to the bitmap data */
54 private long bfOffBits;
56 /** BITMAPFILEHEADER is 14 bytes */
57 public static final int SIZE = 14;
59 /**
60 * Creates the header from an input stream, which is not closed.
61 * @throws IOException if an I/O error occured.
62 * @throws BMPException if the header was invalid
64 public BMPFileHeader(ImageInputStream in) throws IOException, BMPException {
65 byte[] data = new byte[SIZE];
67 if (in.read(data) != SIZE)
68 throw new IOException("Couldn't read header.");
69 ByteBuffer buf = ByteBuffer.wrap(data);
71 if(buf.getShort(0) != bfType)
72 throw new BMPException("Not a BMP file.");
74 buf.order(ByteOrder.LITTLE_ENDIAN);
76 // get size (keep unsigned)
77 bfSize = ((long)buf.getInt(2) & (0xFFFFFFFF));
79 // Two reserved shorts are here, and should be zero,
80 // perhaps they should be tested to be zero, but I don't
81 // feel this strictness is necessary.
83 bfOffBits = ((long)buf.getInt(10) & (0xFFFFFFFF));
86 /**
87 * Writes the header to an output stream, which is not closed or flushed.
88 * @throws IOException if an I/O error occured.
90 public void write(OutputStream out) throws IOException {
91 ByteBuffer buf = ByteBuffer.allocate(SIZE);
92 buf.putShort(0, bfType); // ID
93 buf.putInt(2, (int)(bfSize & (0xFFFFFFFF))); // size
94 buf.putInt(6, 0); // 4 reserved bytes set to zero
95 buf.putInt(2, (int)(bfOffBits & (0xFFFFFFFF))); // size
96 out.write(buf.array());
99 /**
100 * Sets the file size
102 public void setSize(long size){
103 bfSize = size;
107 * Sets the bitmap offset within the file
109 public void setOffset(long offset){
110 bfOffBits = offset;
114 * Gets the file size
116 public long getSize(){
117 return bfSize;
121 * Gets the bitmap offset within the file
123 public long getOffset(){
124 return bfOffBits;