Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 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: 19-Jul-2008
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21
22 /**
23 * A section writer wraps a regular writer so that all the offsets
24 * are relative to the start of a section.
25 *
26 * @author Steve Ratcliffe
27 */
28 public class SectionWriter implements ImgFileWriter {
29
30 private final ImgFileWriter writer;
31 private final Section section;
32 private final int secStart;
33
34 public SectionWriter(ImgFileWriter writer, Section section) {
35 this.writer = writer;
36 this.secStart = section.getPosition();
37 this.section = section;
38 }
39
40 public void sync() throws IOException {
41 writer.sync();
42 }
43
44 /**
45 * Note that this does not close the underlying file.
46 */
47 public void close() {
48 if (section != null)
49 section.setSize(writer.position() - secStart);
50 //writer.close();
51 }
52
53 public int position() {
54 return writer.position() - secStart;
55 }
56
57 public void position(long pos) {
58 writer.position(pos + secStart);
59 }
60
61 public void put(byte b) {
62 writer.put(b);
63 }
64
65 public void put1s(int val) {
66 writer.put1s(val);
67 }
68
69 public void put2s(int val) {
70 writer.put2s(val);
71 }
72
73 public void put3s(int val) {
74 writer.put3s(val);
75 }
76
77 public void put1u(int val) {
78 writer.put1u(val);
79 }
80
81 public void put2u(int val) {
82 writer.put2u(val);
83 }
84
85 public void put3u(int val) {
86 writer.put3u(val);
87 }
88
89 public void putNu(int nBytes, int val) {
90 writer.putNu(nBytes, val);
91 }
92
93 public void put4(int val) {
94 writer.put4(val);
95 }
96
97 public void put(byte[] val) {
98 writer.put(val);
99 }
100
101 public void put(byte[] src, int start, int length) {
102 writer.put(src, start, length);
103 }
104
105 public void put(ByteBuffer src) {
106 writer.put(src);
107 }
108
109 public long getSize() {
110 throw new UnsupportedOperationException("Cannot get size at this point");
111 }
112 }