| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app; |
| 18 |
|
| 19 |
import java.io.Closeable; |
| 20 |
import java.io.IOException; |
| 21 |
|
| 22 |
import uk.me.parabola.imgfmt.Utils; |
| 23 |
import uk.me.parabola.log.Logger; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class ImgFile implements Closeable { |
| 33 |
private static final Logger log = Logger.getLogger(ImgFile.class); |
| 34 |
|
| 35 |
private CommonHeader header; |
| 36 |
|
| 37 |
private ImgFileWriter writer; |
| 38 |
private ImgFileReader reader; |
| 39 |
|
| 40 |
private boolean readable; |
| 41 |
private boolean writable; |
| 42 |
|
| 43 |
public void close() { |
| 44 |
Utils.closeFile(writer); |
| 45 |
Utils.closeFile(reader); |
| 46 |
} |
| 47 |
|
| 48 |
public int position() { |
| 49 |
if (readable) |
| 50 |
return (int) reader.position(); |
| 51 |
else |
| 52 |
return writer.position(); |
| 53 |
} |
| 54 |
|
| 55 |
protected CommonHeader getHeader() { |
| 56 |
return header; |
| 57 |
} |
| 58 |
|
| 59 |
public long getSize() { |
| 60 |
if (writable) |
| 61 |
return writer.getSize(); |
| 62 |
throw new UnsupportedOperationException("getSize not implemented for read"); |
| 63 |
} |
| 64 |
|
| 65 |
protected void position(long pos) { |
| 66 |
writer.position(pos); |
| 67 |
} |
| 68 |
|
| 69 |
protected final void sync() throws IOException { |
| 70 |
if (!writable) |
| 71 |
return; |
| 72 |
getWriter().sync(); |
| 73 |
} |
| 74 |
|
| 75 |
protected ImgFileWriter getWriter() { |
| 76 |
return writer; |
| 77 |
} |
| 78 |
|
| 79 |
protected void setWriter(ImgFileWriter writer) { |
| 80 |
writable = true; |
| 81 |
this.writer = writer; |
| 82 |
} |
| 83 |
|
| 84 |
protected ImgFileReader getReader() { |
| 85 |
return reader; |
| 86 |
} |
| 87 |
|
| 88 |
protected void setReader(ImgFileReader reader) { |
| 89 |
readable = true; |
| 90 |
this.reader = reader; |
| 91 |
} |
| 92 |
|
| 93 |
protected final void setHeader(CommonHeader header) { |
| 94 |
this.header = header; |
| 95 |
} |
| 96 |
|
| 97 |
protected boolean isWritable() { |
| 98 |
return writable; |
| 99 |
} |
| 100 |
} |