View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  /**
29   * CpoByteArrayInputStream is a utility class to help process ByteArrayInputStreams
30   *
31   * @author david berry
32   */
33  public class CpoByteArrayInputStream extends ByteArrayInputStream implements java.io.Serializable, java.lang.Cloneable {
34  
35    /**
36     * Version Id for this class.
37     */
38    private static final long serialVersionUID = 1L;
39    private byte[] buffer_ = null; //The buffer for the byte Array
40    private int offset_ = 0;
41    private int size_ = 0;
42  
43    public CpoByteArrayInputStream(byte[] buffer) {
44      super(buffer);
45      buffer_ =buffer;
46    }
47  
48    public CpoByteArrayInputStream(byte[] buffer, int offset, int length) {
49      super(buffer, offset, length);
50      buffer_ = buffer;
51      offset_ = offset;
52      size_ = length;
53    }
54  
55    protected void setBuffer(byte[] buffer) {
56      buffer_ = buffer;
57    }
58  
59    protected byte[] getBuffer() {
60      return buffer_;
61    }
62  
63    protected void setOffset(int offset) {
64      offset_ = offset;
65    }
66  
67    protected int getOffset() {
68      return offset_;
69    }
70  
71    protected void setSize(int size) {
72      size_ = size;
73    }
74  
75    protected int getSize() {
76      return size_;
77    }
78  
79    public int getLength() {
80      int l;
81  
82      if (getOffset() == 0) {
83        l = getBuffer().length;
84      } else {
85        l = getSize() < getBuffer().length - getOffset() ? getSize() : getBuffer().length - getOffset();
86      }
87      return l;
88    }
89  
90    static public CpoByteArrayInputStream getCpoStream(InputStream is) {
91      CpoByteArrayInputStream cbais = null;
92  
93      if (is instanceof CpoByteArrayInputStream) {
94        cbais = ((CpoByteArrayInputStream) is);
95      } else {
96        // Need to determine the length of the InputStream
97        int b;
98        try {
99          ByteArrayOutputStream baos = new ByteArrayOutputStream();
100         while ((b = is.read()) != -1) {
101           baos.write(b);
102         }
103         cbais = new CpoByteArrayInputStream(baos.toByteArray());
104       } catch (IOException ioe) {
105         // do nothing for now. The null should get someone's attention. 
106       } finally {
107         try {
108           is.close();
109         } catch (IOException ioe) {
110         }
111       }
112 
113     }
114 
115     return cbais;
116   }
117 }