Debian Codegraph @

src/uk/me/parabola/imgfmt/app/ImgFile.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: 03-Dec-2006
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 * Base class for all the img files. There is a common header that
27 * all the sub-files share. They also have means of reading and writing
28 * themselves.
29 *
30 * @author Steve Ratcliffe
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 }