Debian Codegraph @

src/uk/me/parabola/imgfmt/app/CommonHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Dec 14, 2007
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.nio.charset.StandardCharsets;
20 import java.util.Date;
21
22 import uk.me.parabola.imgfmt.ReadFailedException;
23 import uk.me.parabola.imgfmt.Utils;
24
25 /**
26 * The header that is common to all application files within the .img file.
27 * It basically contains two things of interest, the size of the header and
28 * its type. The type is usually of the form 'GARMIN.YYY' where YYY is the
29 * file extension of the type eg TRE, LBL, RGN etc.
30 *
31 * @author Steve Ratcliffe
32 */
33 public abstract class CommonHeader {
34 protected static final int COMMON_HEADER_LEN = 21;
35 private static final int TYPE_LEN = 10;
36
37 // The common header contains the length and the type which are set at
38 // construction time.
39 private int headerLength;
40 private String type;
41
42 // Set to 0x80 on locked maps. We are not interested in creating locked
43 // maps, but may be useful to recognise them for completeness.
44 private byte lockFlag;
45
46 // A date of creation.
47 private Date creationDate;
48
49 protected CommonHeader(int headerLength, String type) {
50 this.headerLength = headerLength;
51 this.type = type;
52 }
53
54 /**
55 * Writes out the header that is common to all the file types. It should
56 * be called by the sync() methods of subclasses when they are ready.
57 * @param writer Used to write the header.
58 */
59 public final void writeHeader(ImgFileWriter writer) {
60 writePrepare();
61
62 writer.position(0);
63
64 writer.put2u(headerLength);
65 writer.put(Utils.toBytes(type, TYPE_LEN, (byte) 0));
66
67 writer.put1u(1); // unknown
68 writer.put1u(0); // not locked
69
70 byte[] date = Utils.makeCreationTime(new Date());
71 writer.put(date);
72
73 writeFileHeader(writer);
74 }
75
76 /**
77 * Read the common header. It starts at the beginning of the file.
78 * @param reader Used to read the header.
79 */
80 public final void readHeader(ImgFileReader reader) throws ReadFailedException {
81 reader.position(reader.getGMPOffset());
82 headerLength = reader.get2u();
83 byte[] bytes = reader.get(TYPE_LEN);
84 type = new String(bytes, StandardCharsets.US_ASCII);
85 reader.get(); // ignore
86 this.lockFlag = reader.get(); // 0x80 if locked
87
88 byte[] date = reader.get(7);
89 creationDate = Utils.makeCreationTime(date);
90
91 readFileHeader(reader);
92 }
93
94 /**
95 * Read the rest of the header. Specific to the given file. It is
96 * guaranteed that the file position will be set to the correct place
97 * before this is called.
98 * @param reader The header is read from here.
99 */
100 protected abstract void readFileHeader(ImgFileReader reader) throws ReadFailedException;
101
102 /**
103 * Write the rest of the header. It is guaranteed that the writer will
104 * be set to the correct position before calling.
105 * @param writer The header is written here.
106 */
107 protected abstract void writeFileHeader(ImgFileWriter writer);
108
109 public int getHeaderLength() {
110 return headerLength;
111 }
112
113 private void writePrepare() {
114 // Prepare for write by setting our defaults.
115 // lockFlag = 0;
116 if (creationDate == null)
117 creationDate = new Date();
118 }
119
120 /**
121 * @return the lockFlag
122 */
123 public byte getLockFlag() {
124 return lockFlag;
125 }
126 }