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