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.CharArrayReader;
24  import java.io.CharArrayWriter;
25  import java.io.IOException;
26  import java.io.Reader;
27  
28  /**
29   * CpoCharArrayReader is a utility class to help process CharArrayReader
30   *
31   * @author david berry
32   */
33  public class CpoCharArrayReader extends CharArrayReader 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 char[] buffer_ = null; //The buffer for the byte Array
40    private int offset_ = 0;
41    private int size_ = 0;
42  
43    public CpoCharArrayReader(char[] buffer) {
44      super(buffer);
45      buffer_ = buffer;
46    }
47  
48    public CpoCharArrayReader(char[] 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(char[] buffer) {
56      buffer_ = buffer;
57    }
58  
59    protected char[] 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 CpoCharArrayReader getCpoReader(Reader r) {
91      CpoCharArrayReader ccar = null;
92      if (r instanceof CpoCharArrayReader) {
93        ccar = ((CpoCharArrayReader) r);
94      } else {
95        // Need to determine the length of the Reader
96        // Need to determine the length of the InputStream
97        int b;
98        try {
99          CharArrayWriter caw = new CharArrayWriter();
100         while ((b = r.read()) != -1) {
101           caw.write(b);
102         }
103         ccar = new CpoCharArrayReader(caw.toCharArray());
104       } catch (IOException ioe) {
105         // do nothing for now. The null should get someone's attention. 
106       } finally {
107         try {
108           r.close();
109         } catch (IOException ioe) {
110         }
111       }
112     }
113 
114     return ccar;
115   }
116 }