Debian Codegraph @

test/func/lib/ArrayImgWriter.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 func.lib;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18
19 import uk.me.parabola.imgfmt.app.ImgFileWriter;
20
21 /**
22 * A writer that just writes to an array for testing.
23 *
24 * @author Steve Ratcliffe
25 */
26 public class ArrayImgWriter implements ImgFileWriter {
27 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
28
29 public void sync() throws IOException {
30 }
31
32 public int position() {
33 return out.size();
34 }
35
36 public void position(long pos) {
37 throw new UnsupportedOperationException();
38 }
39
40 public void put(byte b) {
41 out.write(b);
42 }
43
44 public void put1s(int val) {
45 assert val >= -128 && val <= 127 : val;
46 out.write(val);
47 }
48
49 public void put2s(int val) {
50 assert val >= -32768 && val <= 32767 : val;
51 out.write(val);
52 out.write(val >> 8);
53 }
54
55 public void put3s(int val) {
56 assert val >= -0x800000 && val <= 0x7fffff : val;
57 out.write(val);
58 out.write(val >> 8);
59 out.write(val >> 16);
60 }
61
62 public void put1u(int val) {
63 assert val >= 0 && val <= 255 : val;
64 out.write(val);
65 }
66
67 public void put2u(int val) {
68 assert val >= 0 && val <= 65535 : val;
69 out.write(val);
70 out.write(val >> 8);
71 }
72
73 public void put3u(int val) {
74 assert val >= 0 && val <= 0xffffff : val;
75 out.write(val);
76 out.write(val >> 8);
77 out.write(val >> 16);
78 }
79
80 public void putNu(int nBytes, int val) {
81 out.write(val);
82 if (nBytes <= 1) {
83 assert val >= 0 && val <= 255 : val;
84 return;
85 }
86 out.write(val >> 8);
87 if (nBytes <= 2) {
88 assert val >= 0 && val <= 65535 : val;
89 return;
90 }
91 out.write(val >> 16);
92 if (nBytes <= 3) {
93 assert val >= 0 && val <= 0xffffff : val;
94 return;
95 }
96 out.write(val >> 24);
97 }
98
99 public void put4(int val) {
100 out.write(val);
101 out.write(val >> 8);
102 out.write(val >> 16);
103 out.write(val >> 24);
104 }
105
106 public void put(byte[] val) {
107 out.write(val, 0, val.length);
108 }
109
110 public void put(byte[] src, int start, int length) {
111 out.write(src, start, length);
112 }
113
114 public void put(ByteBuffer src) {
115 byte[] array = src.array();
116 out.write(array, 0, src.limit());
117 }
118
119 public long getSize() {
120 return out.size();
121 }
122
123 public void close() throws IOException {
124 out.close();
125 }
126
127 public byte[] getBytes() {
128 return out.toByteArray();
129 }
130 }