| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 122 |
|
| 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 |
|
| 135 |
|
| 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 |
|
| 149 |
|
| 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 |
|
| 164 |
|
| 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 |
|
| 177 |
|
| 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 |
|
| 191 |
|
| 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 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public void close() throws IOException { |
| 316 |
sync(); |
| 317 |
} |
| 318 |
} |