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 *
020 * <h5 class='section'>See Also:</h5><ul>
021
022 * </ul>
023 */
024public final class CharSequenceReader extends BufferedReader {
025
026   private final CharSequence cs;
027   private String s;
028   private StringBuffer sb;
029   private StringBuilder sb2;
030   private int length;
031   private int next = 0;
032
033   /**
034    * Constructor.
035    *
036    * @param cs The char sequence to read from.  Can be <jk>null</jk>.
037    */
038   public CharSequenceReader(CharSequence cs) {
039      super(new StringReader(""), 1);   // Does not actually use a reader.
040      if (cs == null)
041         cs = "";
042      this.cs = cs;
043      if (cs instanceof String)
044         s = (String)cs;
045      else if (cs instanceof StringBuffer)
046         sb = (StringBuffer)cs;
047      else if (cs instanceof StringBuilder)
048         sb2 = (StringBuilder)cs;
049      this.length = cs.length();
050   }
051
052   @Override /* Reader */
053   public int read() {
054      if (next >= length)
055         return -1;
056      return cs.charAt(next++);
057   }
058
059   @Override /* Reader */
060   public boolean markSupported() {
061      return false;
062   }
063
064   @Override /* Reader */
065   public int read(final char[] cbuf, final int off, final int len) {
066      if (next >= length)
067         return -1;
068      int n = Math.min(length - next, len);
069      if (s != null)
070         s.getChars(next, next + n, cbuf, off);
071      else if (sb != null)
072         sb.getChars(next, next + n, cbuf, off);
073      else if (sb2 != null)
074         sb2.getChars(next, next + n, cbuf, off);
075      else {
076         for (int i = 0; i < n; i++)
077            cbuf[off+i] = cs.charAt(next+i);
078      }
079      next += n;
080      return n;
081   }
082
083   @Override /* Reader */
084   public long skip(long ns) {
085      if (next >= length)
086         return 0;
087      long n = Math.min(length - next, ns);
088      n = Math.max(-next, n);
089      next += n;
090      return n;
091   }
092
093   @Override /* Reader */
094   public void close() {
095      // no-op
096   }
097
098   @Override /* Object */
099   public String toString() {
100      return cs.toString();
101   }
102}