001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import java.io.*;
016
017/**
018 * Similar to {@link StringReader} except reads from a generic {@link CharSequenceReader}.
019 */
020public final class CharSequenceReader extends BufferedReader {
021
022   private final CharSequence cs;
023   private String s;
024   private StringBuffer sb;
025   private StringBuilder sb2;
026   private int length;
027   private int next = 0;
028
029   /**
030    * Constructor.
031    *
032    * @param cs The char sequence to read from.  Can be <jk>null</jk>.
033    */
034   public CharSequenceReader(CharSequence cs) {
035      super(new StringReader(""), 1);   // Does not actually use a reader.
036      if (cs == null)
037         cs = "";
038      this.cs = cs;
039      if (cs instanceof String)
040         s = (String)cs;
041      else if (cs instanceof StringBuffer)
042         sb = (StringBuffer)cs;
043      else if (cs instanceof StringBuilder)
044         sb2 = (StringBuilder)cs;
045      this.length = cs.length();
046   }
047
048   @Override /* Reader */
049   public int read() {
050      if (next >= length)
051         return -1;
052      return cs.charAt(next++);
053   }
054
055   @Override /* Reader */
056   public boolean markSupported() {
057      return false;
058   }
059
060   @Override /* Reader */
061   public int read(final char[] cbuf, final int off, final int len) {
062      if (next >= length)
063         return -1;
064      int n = Math.min(length - next, len);
065      if (s != null)
066         s.getChars(next, next + n, cbuf, off);
067      else if (sb != null)
068         sb.getChars(next, next + n, cbuf, off);
069      else if (sb2 != null)
070         sb2.getChars(next, next + n, cbuf, off);
071      else {
072         for (int i = 0; i < n; i++)
073            cbuf[off+i] = cs.charAt(next+i);
074      }
075      next += n;
076      return n;
077   }
078
079   @Override /* Reader */
080   public long skip(long ns) {
081      if (next >= length)
082         return 0;
083      long n = Math.min(length - next, ns);
084      n = Math.max(-next, n);
085      next += n;
086      return n;
087   }
088
089   @Override /* Reader */
090   public void close() {
091      // no-op
092   }
093
094   @Override /* Object */
095   public String toString() {
096      return cs.toString();
097   }
098}