Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app;
14
15 import java.io.BufferedOutputStream;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.channels.FileChannel;
22
23 import uk.me.parabola.imgfmt.MapFailedException;
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 * Write img file data to a temporary file. On a call to sync() the data
31 * is copied to the output channel.
32 *
33 * @author Steve Ratcliffe
34 */
35 public class FileBackedImgFileWriter implements ImgFileWriter, Sized {
36 private final ImgChannel outputChan;
37 private final File tmpFile;
38 private final BufferedOutputStream file;
39 private final FileChannel tmpChannel;
40 private long finalSize;
41
42 public FileBackedImgFileWriter(ImgChannel chan, File outputDir) {
43 this.outputChan = chan;
44
45 try {
46 tmpFile = File.createTempFile("img", null, outputDir);
47 tmpFile.deleteOnExit();
48
49 FileOutputStream out = new FileOutputStream(tmpFile);
50 tmpChannel = out.getChannel();
51 file = new BufferedOutputStream(out, 16*1024);
52 } catch (IOException e) {
53 throw new MapFailedException("Could not create temporary file");
54 }
55
56 if (chan instanceof FileLink) {
57 ((FileLink) chan).link(this, this);
58 }
59 }
60
61 /**
62 * Maps the temporary file and copies to the output channel.
63 *
64 * @throws IOException If there is an error writing.
65 */
66 public void sync() throws IOException {
67 finalSize = getSize();
68 file.close();
69
70 try (FileInputStream is = new FileInputStream(tmpFile); FileChannel channel = is.getChannel()) {
71 channel.transferTo(0, channel.size(), outputChan);
72 } finally {
73 if (!tmpFile.delete())
74 Logger.defaultLogger.error("could not delete temporary file " + tmpFile.getPath());
75 }
76 }
77
78 /**
79 * Get the position. Have to flush the buffer before getting the position.
80 *
81 * @return The logical position within the file.
82 */
83 public int position() {
84 try {
85 file.flush();
86 return (int) tmpChannel.position();
87 } catch (IOException e) {
88 return 0;
89 }
90 }
91
92 /**
93 * Set the position of the file.
94 * The buffer has to be flushed first.
95 *
96 * @param pos The new position in the file.
97 */
98 public void position(long pos) {
99 try {
100 file.flush();
101 tmpChannel.position(pos);
102 } catch (IOException e) {
103 throw new MapFailedException("could not set position in temporary file " + tmpFile.getPath());
104 }
105 }
106
107 /**
108 * Write out a single byte.
109 *
110 * @param b The byte to write.
111 */
112 public void put(byte b) {
113 try {
114 file.write(b);
115 } catch (IOException e) {
116 throw new MapFailedException("could not write byte to mdr tmp file");
117 }
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 try {
127 file.write(val);
128 } catch (IOException e) {
129 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
130 }
131 }
132
133 /**
134 * Write out int in range -32768..32767 as two bytes in little endian byte order.
135 * @param val The value to write.
136 */
137 public void put2s(int val) {
138 assert val >= -32768 && val <= 32767 : val;
139 try {
140 file.write(val);
141 file.write(val >> 8);
142 } catch (IOException e) {
143 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
144 }
145 }
146
147 /**
148 * Write out int in range -0x800000..0x7fffff in little endian byte order.
149 * @param val The value to write.
150 */
151 public void put3s(int val) {
152 assert val >= -0x800000 && val <= 0x7fffff : val;
153 try {
154 file.write(val);
155 file.write(val >> 8);
156 file.write(val >> 16);
157 } catch (IOException e) {
158 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
159 }
160 }
161
162 /**
163 * Write out int in range 0..255 as single byte.
164 * @param val The value to write.
165 */
166 public void put1u(int val) {
167 assert val >= 0 && val <= 255 : val;
168 try {
169 file.write(val);
170 } catch (IOException e) {
171 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
172 }
173 }
174
175 /**
176 * Write out int in range 0..65535 as two bytes in little endian byte order.
177 * @param val The value to write.
178 */
179 public void put2u(int val) {
180 assert val >= 0 && val <= 65535 : val;
181 try {
182 file.write(val);
183 file.write(val >> 8);
184 } catch (IOException e) {
185 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
186 }
187 }
188
189 /**
190 * Write out int in range 0..0xffffff as three bytes in little endian byte order.
191 * @param val The value to write.
192 */
193 public void put3u(int val) {
194 assert val >= 0 && val <= 0xffffff : val;
195 try {
196 file.write(val);
197 file.write(val >> 8);
198 file.write(val >> 16);
199 } catch (IOException e) {
200 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
201 }
202 }
203
204 /**
205 * Write out int as 1-4 bytes in little endian byte order.
206 *
207 * @param nBytes The number of bytes to write.
208 * @param val The value to write. Unsigned
209 */
210 public void putNu(int nBytes, int val) {
211 assert nBytes >= 1 && nBytes <= 4: nBytes;
212 try {
213 file.write(val);
214 if (nBytes <= 1) {
215 assert val >= 0 && val <= 255 : val;
216 return;
217 }
218 file.write(val >> 8);
219 if (nBytes <= 2) {
220 assert val >= 0 && val <= 65535 : val;
221 return;
222 }
223 file.write(val >> 16);
224 if (nBytes <= 3) {
225 assert val >= 0 && val <= 0xffffff : val;
226 return;
227 }
228 file.write(val >> 24);
229 } catch (IOException e) {
230 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
231 }
232 }
233
234 /**
235 * Write out 4 byte (signed or unsigned) value.
236 *
237 * @param val The value to write.
238 */
239 public void put4(int val) {
240 try {
241 file.write(val);
242 file.write(val >> 8);
243 file.write(val >> 16);
244 file.write(val >> 24);
245 } catch (IOException e) {
246 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
247 }
248 }
249
250 /**
251 * Write out an arbitrary length sequence of bytes.
252 *
253 * @param val The values to write.
254 */
255 public void put(byte[] val) {
256 try {
257 file.write(val);
258 } catch (IOException e) {
259 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
260 }
261 }
262
263 /**
264 * Write out part of a byte array.
265 *
266 * @param src The array to take bytes from.
267 * @param start The start position.
268 * @param length The number of bytes to write.
269 */
270 public void put(byte[] src, int start, int length) {
271 try {
272 file.write(src, start, length);
273 } catch (IOException e) {
274 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
275 }
276 }
277
278 /**
279 * Write out a complete byte buffer.
280 *
281 * @param src The buffer to write.
282 */
283 public void put(ByteBuffer src) {
284 try {
285 file.flush();
286 tmpChannel.write(src);
287 } catch (IOException e) {
288 throw new MapFailedException("could not write to temporary file " + tmpFile.getPath());
289 }
290 }
291
292 /**
293 * Returns the size of the file.
294 *
295 * @return The file size in bytes.
296 */
297 public long getSize() {
298 if (finalSize > 0)
299 return finalSize;
300
301 try {
302 file.flush();
303 return tmpChannel.size();
304 } catch (IOException e) {
305 throw new MapFailedException("could not get size of temporary file " + tmpFile.getPath());
306 }
307 }
308
309 /**
310 * Closes this stream with the result that the contents of the temporary file are written to the
311 * real output.
312 *
313 * @throws IOException if an I/O error occurs
314 */
315 public void close() throws IOException {
316 sync();
317 }
318 }