1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.jpeg.segments;
18
19 import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.PrintWriter;
24 import java.nio.charset.Charset;
25
26 public abstract class GenericSegment extends AbstractSegment {
27 private final byte[] segmentData;
28
29 public GenericSegment(final int marker, final byte[] bytes) {
30 super(marker, bytes.length);
31
32 this.segmentData = bytes.clone();
33 }
34
35 public GenericSegment(final int marker, final int markerLength, final InputStream is) throws IOException {
36 super(marker, markerLength);
37
38 segmentData = readBytes("Segment Data", is, markerLength, "Invalid Segment: insufficient data");
39 }
40
41 @Override
42 public void dump(final PrintWriter pw) {
43 dump(pw, 0);
44 }
45
46 public void dump(final PrintWriter pw, final int start) {
47 for (int i = 0; i < 50 && i + start < segmentData.length; i++) {
48 debugNumber(pw, "\t" + (i + start), segmentData[i + start], 1);
49 }
50 }
51
52
53
54
55
56
57 public byte[] getSegmentData() {
58 return segmentData.clone();
59 }
60
61
62
63
64
65
66
67
68 protected byte getSegmentData(final int offset) {
69 return segmentData[offset];
70 }
71
72
73
74
75
76
77
78 public String getSegmentDataAsString(final Charset encoding) {
79 return new String(segmentData, encoding);
80 }
81
82 }