001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020
021package org.sonar.squid.text;
022
023import java.io.IOException;
024import java.io.Reader;
025import java.io.StringReader;
026
027public class StringArrayReader extends Reader {
028
029  private final StringReader stringReader;
030
031  enum EndOfLineDelimiter {
032    LF, CR_PLUS_LF, CR
033  }
034
035  public StringArrayReader(String[] lines) {
036    this(lines, EndOfLineDelimiter.LF);
037  }
038
039  public StringArrayReader(String[] lines, EndOfLineDelimiter endOfLineDelimiter) {
040    if (lines == null) {
041      throw new IllegalStateException("lines object can't be null.");
042    }
043    String content = convertArrayToStringAndAppendEndOfLine(lines, endOfLineDelimiter);
044    stringReader = new StringReader(content);
045  }
046
047  private String convertArrayToStringAndAppendEndOfLine(String[] lines, EndOfLineDelimiter endOfLineDelimiter) {
048    StringBuilder content = new StringBuilder();
049    for (int i = 0; i < lines.length; i++) {
050      content.append(lines[i]);
051      if (i != (lines.length - 1)) {
052        switch (endOfLineDelimiter) {
053          case LF:
054            content.append('\n');
055            break;
056          case CR:
057            content.append('\r');
058            break;
059          case CR_PLUS_LF:
060            content.append("\r\n");
061            break;
062          default:
063            throw new IllegalStateException(); // should never happen
064        }
065      }
066    }
067    return content.toString();
068  }
069
070  @Override
071  public void close() throws IOException {
072    stringReader.close();
073  }
074
075  @Override
076  public boolean ready() throws IOException {
077    return stringReader.ready();
078  }
079
080  @Override
081  public boolean markSupported() {
082    return stringReader.markSupported();
083  }
084
085  @Override
086  public void mark(int readAheadLimit) throws IOException {
087    stringReader.mark(readAheadLimit);
088  }
089
090  @Override
091  public void reset() throws IOException {
092    stringReader.reset();
093  }
094
095  @Override
096  public int read(char[] cbuf, int off, int len) throws IOException {
097    return stringReader.read(cbuf, off, len);
098  }
099}