Debian Codegraph @

src/uk/me/parabola/imgfmt/app/Section.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 15, 2007
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.io.IOException;
20
21 /**
22 * Represents an item size the position where those items start and the
23 * total size of the section.
24 */
25 public class Section {
26 private int itemSize;
27 private int size;
28 private int position;
29 private Section link;
30 private int extraValue;
31
32 public Section() {
33 }
34
35 public Section(int itemSize) {
36 this.itemSize = itemSize;
37 }
38
39 public Section(Section link, int itemSize) {
40 this.itemSize = itemSize;
41 this.link = link;
42 }
43
44 public Section(Section link) {
45 this.link = link;
46 }
47
48 public void inc() {
49 size += itemSize;
50 }
51
52 public int getItemSize() {
53 return itemSize;
54 }
55
56 public void setItemSize(int itemSize) {
57 this.itemSize = itemSize;
58 }
59
60 public int getSize() {
61 return size;
62 }
63
64 public void setSize(int size) {
65 this.size = size;
66 }
67
68 /**
69 * Get the start position of this section. If this is linked to another
70 * section, then we return the end address of that section.
71 * @return The first offset for this section.
72 */
73 public int getPosition() {
74 if (link != null)
75 return link.getEndPos();
76 return position;
77 }
78
79 public void setPosition(int position) {
80 this.position = position;
81 // Setting a position breaks the link
82 this.link = null;
83 }
84
85 /**
86 * Get the position of the end of the section.
87 * @return The offset of the end of the section relative to the beginning
88 * of the application file.
89 */
90 public int getEndPos() {
91 return getPosition() + size;
92 }
93
94 public String toString() {
95 return "pos=" + getPosition() + ", size=" + size + ", itemSize=" + itemSize;
96 }
97
98 /**
99 * Get the number of items in the section. This should only be called
100 * if the itemSize is set.
101 * @return The number of items in the section, or zero if this is not
102 * a fixed size item kind of section.
103 */
104 public int getNumItems() {
105 if (itemSize == 0)
106 return 0;
107
108 return size/itemSize;
109 }
110
111 public int getExtraValue() {
112 return extraValue;
113 }
114
115 public void setExtraValue(int extraValue) {
116 this.extraValue = extraValue;
117 }
118
119 public void readSectionInfo(ImgFileReader reader, boolean withItemSize) {
120 readSectionInfo(reader, withItemSize, false);
121 }
122
123 public void readSectionInfo(ImgFileReader reader, boolean withItemSize, boolean withExtraValue) {
124 setPosition(reader.get4());
125 setSize(reader.get4());
126 if (withItemSize)
127 setItemSize(reader.get2u());
128 if (withExtraValue)
129 setExtraValue(reader.get4());
130 }
131
132 public SectionWriter makeSectionWriter(ImgFileWriter writer) {
133 setPosition(writer.position());
134 return new SectionWriter(writer, this);
135 }
136
137 public void writeSectionInfo(ImgFileWriter writer) {
138 writeSectionInfo(writer, false);
139 }
140
141 public void writeSectionInfo(ImgFileWriter writer, boolean withItemSize) {
142 writeSectionInfo(writer, withItemSize, false);
143 }
144
145 public void writeSectionInfo(ImgFileWriter writer, boolean withItemSize, boolean withExtraValue) {
146 writer.put4(getPosition());
147 writer.put4(getSize());
148 if (withItemSize || getItemSize() > 0)
149 writer.put2u(getItemSize());
150 if (withExtraValue)
151 writer.put4(getExtraValue());
152 }
153
154 public static void close(ImgFileWriter writer) {
155 assert writer instanceof SectionWriter;
156 try {
157 writer.close();
158 } catch (IOException ignore) {
159 // ignore as this is only for section writers.
160 }
161 }
162 }