Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 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: 07-Dec-2006
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22
23 import uk.me.parabola.imgfmt.MapTooBigException;
24 import uk.me.parabola.imgfmt.Sized;
25 import uk.me.parabola.imgfmt.fs.ImgChannel;
26 import uk.me.parabola.imgfmt.sys.FileLink;
27 import uk.me.parabola.log.Logger;
28
29 /**
30 * A straight forward implementation that just keeps all the data in a buffer
31 * until the file needs to be written to disk.
32 *
33 * @author Steve Ratcliffe
34 */
35 public class BufferedImgFileWriter implements ImgFileWriter, Sized {
36 private static final Logger log = Logger.getLogger(BufferedImgFileWriter.class);
37
38 private static final int KBYTE = 1024;
39 private static final int INIT_SIZE = 16 * KBYTE;
40 private static final int GROW_SIZE = 128 * KBYTE;
41 private static final int GUARD_SIZE = KBYTE;
42
43 private final ImgChannel chan;
44 private final String subfile;
45
46 private ByteBuffer buf = ByteBuffer.allocate(INIT_SIZE);
47 private int bufferSize = INIT_SIZE;
48
49 // The size of the file. Note that for this to be set properly, the
50 // position must be set to a low value after the full file is written. This
51 // always happens because we go back and write the header after all is
52 // written.
53 // Always use getSize() which takes care of things.
54 private int maxSize;
55
56 // The maximum allowed file size.
57 private long maxAllowedSize = 0xffffff;
58
59 public BufferedImgFileWriter(ImgChannel chan, String subfile) {
60 this.chan = chan;
61 this.subfile = subfile;
62 buf.order(ByteOrder.LITTLE_ENDIAN);
63 if (chan instanceof FileLink) {
64 ((FileLink) chan).link(this, this);
65 }
66 }
67
68 /**
69 * Called to write out any saved buffers. The strategy may write
70 * directly to the file in which case this would have nothing or
71 * little to do.
72 */
73 public void sync() throws IOException {
74 buf.limit((int) getSize());
75 buf.position(0);
76 log.debug("syncing to pos", chan.position(), ", size", buf.limit());
77 chan.write(buf);
78 }
79
80 /**
81 * Get the position. Needed because may not be reflected in the underlying
82 * file if being buffered.
83 *
84 * @return The logical position within the file.
85 */
86 public int position() {
87 return buf.position();
88 }
89
90 /**
91 * Set the position of the file.
92 *
93 * @param pos The new position in the file.
94 */
95 public void position(long pos) {
96 int cur = position();
97 if (cur > maxSize)
98 maxSize = cur;
99 buf.position((int) pos);
100 }
101
102 /**
103 * Called when the stream is closed.
104 */
105 public void close() throws IOException {
106 sync();
107 }
108
109 /**
110 * Write out a single byte.
111 * Should not be used for writing numbers, use put1s/u instead.
112 *
113 * @param b The byte to write.
114 */
115 public void put(byte b) {
116 ensureSize(1);
117 buf.put(b);
118 }
119
120 /**
121 * Write out int in range -128..127 as single byte.
122 * @param val The byte to write.
123 */
124 public void put1s(int val) {
125 assert val >= -128 && val <= 127 : val;
126 ensureSize(1);
127 buf.put((byte)val);
128 }
129
130 /**
131 * Write out int in range -32768..32767 as two bytes in little endian byte order.
132 * @param val The value to write.
133 */
134 public void put2s(int val) {
135 assert val >= -32768 && val <= 32767 : val;
136 ensureSize(2);
137 buf.putShort((short)val);
138 }
139
140 /**
141 * Write out int in range -0x800000..0x7fffff in little endian byte order.
142 * @param val The value to write.
143 */
144 public void put3s(int val) {
145 assert val >= -0x800000 && val <= 0x7fffff : val;
146 ensureSize(3);
147 buf.put((byte)val);
148 buf.putShort((short)(val >> 8));
149 }
150
151 /**
152 * Write out int in range 0..255 as single byte.
153 * @param val The value to write.
154 */
155 public void put1u(int val) {
156 assert val >= 0 && val <= 255 : val;
157 ensureSize(1);
158 buf.put((byte)val);
159 }
160
161 /**
162 * Write out int in range 0..65535 as two bytes in little endian byte order.
163 * @param val The value to write.
164 */
165 public void put2u(int val) {
166 assert val >= 0 && val <= 65535 : val;
167 ensureSize(2);
168 buf.putChar((char)val);
169 }
170
171 /**
172 * Write out int in range 0..0xffffff as three bytes in little endian byte order.
173 * @param val The value to write.
174 */
175 public void put3u(int val) {
176 assert val >= 0 && val <= 0xffffff : val;
177 ensureSize(3);
178 buf.put((byte)val);
179 buf.putChar((char)(val >> 8));
180 }
181
182 /**
183 * Write out int as 1-4 bytes in little endian byte order.
184 *
185 * @param nBytes The number of bytes to write.
186 * @param val The value to write. Unsigned
187 */
188 public void putNu(int nBytes, int val) {
189 ensureSize(nBytes);
190 switch (nBytes) {
191 case 1:
192 assert val >= 0 && val <= 255 : val;
193 put((byte)val);
194 break;
195 case 2:
196 assert val >= 0 && val <= 65535 : val;
197 buf.putShort((short)val);
198 break;
199 case 3:
200 assert val >= 0 && val <= 0xffffff : val;
201 buf.put((byte)val);
202 buf.putShort((short)(val >> 8));
203 break;
204 case 4:
205 buf.putInt(val);
206 break;
207 default:
208 assert false : nBytes;
209 }
210 }
211
212 /**
213 * Write out int (signed or unsigned) as 4 bytes.
214 *
215 * @param val The value to write.
216 */
217 public void put4(int val) {
218 ensureSize(4);
219 buf.putInt(val);
220 }
221
222 /**
223 * Write out an arbitrary length sequence of bytes.
224 *
225 * @param val The values to write.
226 */
227 public void put(byte[] val) {
228 ensureSize(val.length);
229 buf.put(val);
230 }
231
232 /**
233 * Write out part of a byte array.
234 *
235 * @param src The array to take bytes from.
236 * @param start The start position.
237 * @param length The number of bytes to write.
238 */
239 public void put(byte[] src, int start, int length) {
240 ensureSize(length);
241 buf.put(src, start, length);
242 }
243
244 public void put(ByteBuffer src) {
245 ensureSize(src.limit());
246 buf.put(src);
247 }
248
249 /**
250 * Get the size of the file as written.
251 *
252 * NOTE: that calling this is only valid at certain times.
253 *
254 * @return The size of the file, if it is available.
255 */
256 public long getSize() {
257 return Math.max(maxSize, position());
258 }
259
260 public ByteBuffer getBuffer() {
261 return buf;
262 }
263
264 /**
265 * Make sure there is enough room for the data we are about to write.
266 *
267 * @param length The amount of data.
268 */
269 private void ensureSize(int length) {
270 int needed = buf.position() + length;
271 if (needed > (bufferSize - GUARD_SIZE)) {
272 while(needed > (bufferSize - GUARD_SIZE))
273 bufferSize += GROW_SIZE;
274 if (bufferSize > maxAllowedSize) {
275 throw new MapTooBigException(maxAllowedSize,
276 "The " + subfile + " section of the map or tile ("
277 + log.threadTag()
278 + ") is too big.",
279 "Try splitting the map into smaller tiles or reducing the amount of information included in the map.");
280 }
281 ByteBuffer newb = ByteBuffer.allocate(bufferSize);
282 newb.order(ByteOrder.LITTLE_ENDIAN);
283 buf.flip();
284 newb.put(buf);
285 buf = newb;
286 }
287 }
288
289 public void setMaxSize(long maxSize) {
290 this.maxAllowedSize = maxSize;
291 }
292 }