View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * Returns a copy of the segment's contents, excluding the marker and length bytes at the beginning.
54       *
55       * @return the segment's contents
56       */
57      public byte[] getSegmentData() {
58          return segmentData.clone();
59      }
60  
61      /**
62       * Returns a specific byte of the segment's contents, excluding the marker and length bytes at the beginning.
63       *
64       * @param offset segment offset
65       * @see GenericSegment#getSegmentData()
66       * @return the bye in the segment's contents
67       */
68      protected byte getSegmentData(final int offset) {
69          return segmentData[offset];
70      }
71  
72      /**
73       * Convert the bytes to a String
74       *
75       * @param encoding segment encoding
76       * @return the encoded bytes
77       */
78      public String getSegmentDataAsString(final Charset encoding) {
79          return new String(segmentData, encoding);
80      }
81  
82  }