e81c79e17e0ffeb05c2df398161924e987cbc421
[jalviewjs.git] / src / javajs / util / CompoundDocHeader.java
1 /* $RCSfile$
2  * $Author$
3  * $Date$
4  * $Revision$
5  *
6  * Copyright (C) 2011  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  *  02110-1301, USA.
24  */
25
26 package javajs.util;
27
28
29 class CompoundDocHeader {
30
31   /**
32    * 
33    */
34   private final CompoundDocument cd;
35
36   /**
37    * @param compoundDocument
38    */
39   CompoundDocHeader(CompoundDocument compoundDocument) {
40     cd = compoundDocument;
41   }
42
43   //512 bytes
44   //offset 0:
45   byte[] magicNumbers = new byte[8]; // D0CF11E0A1B11AE1
46   byte[] uniqueID16 = new byte[16];
47   byte revNumber; // 3E = 62
48   //byte unusedb1;
49   byte verNumber; // 3
50   //byte unusedb2;
51   //short byteOrder; // -2 littleEndian
52   short sectorPower; // 2^sectorPower = sector size; 512 = 2^9
53   short shortSectorPower; // 2^shortSectorPower = short sector size; 64 = 2^6
54   byte[] unused = new byte[10];
55   int nSATsectors; // number of sectors for sector allocation table
56   int SID_DIR_start; // sector identifier of start of directory sector
57   //offset 56:
58   int minBytesStandardStream; // less than this (and not DIR) will be "short"
59   int SID_SSAT_start; // start of short sector allocation table (SSAT)
60   int nSSATsectors; // number of sectors allocated to SSAT
61   int SID_MSAT_next; // pointer to next master sector allocation table sector
62   int nAdditionalMATsectors; // number of sectors allocated to more MSAT sectors
63   //offset 76; 436 bytes:      
64   int[] MSAT0 = new int[109]; // beginning of master allocation table 
65
66   /*
67    *  Sector 0 is first sector AFTER this header
68    *  
69    *  If sectorPower = 9, then this allows for 109 PAGES
70    *  of sector allocation tables, with 127 pointers per
71    *  page (plus 1 pointer to the next SAT page), each 
72    *  pointing to a sector of 512 bytes. Thus, with no additional
73    *  MSAT pages, the header allows for 109*128*512 = 7.1 Mb file
74    *  
75    */
76
77   final boolean readData() {
78     try {
79       cd.readByteArray(magicNumbers, 0, 8);
80       if (magicNumbers[0] != (byte) 0xD0 || magicNumbers[1] != (byte) 0xCF
81           || magicNumbers[2] != (byte) 0x11 || magicNumbers[3] != (byte) 0xE0
82           || magicNumbers[4] != (byte) 0xA1 || magicNumbers[5] != (byte) 0xB1
83           || magicNumbers[6] != (byte) 0x1A || magicNumbers[7] != (byte) 0xE1)
84         return false;
85       cd.readByteArray(uniqueID16, 0, 16);
86       revNumber = cd.readByte();
87       cd.readByte();
88       verNumber = cd.readByte();
89       cd.readByte();
90       byte b1 = cd.readByte();
91       byte b2 = cd.readByte();
92       cd.isBigEndian = (b1 == -1 && b2 == -2);
93       sectorPower = cd.readShort();
94       shortSectorPower = cd.readShort();
95       cd.readByteArray(unused, 0, 10);
96       nSATsectors = cd.readInt();
97       SID_DIR_start = cd.readInt();
98       cd.readByteArray(unused, 0, 4);
99       minBytesStandardStream = cd.readInt();
100       SID_SSAT_start = cd.readInt();
101       nSSATsectors = cd.readInt();
102       SID_MSAT_next = cd.readInt();
103       nAdditionalMATsectors = cd.readInt();
104       for (int i = 0; i < 109; i++)
105         MSAT0[i] = cd.readInt();
106     } catch (Exception e) {
107       System.out.println(e.toString());
108       return false;
109     }
110     return true;
111   }
112 }