001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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    package org.sonar.squid.text;
021    
022    import java.io.IOException;
023    import java.io.Reader;
024    import java.io.StringReader;
025    
026    public class StringArrayReader extends Reader {
027    
028      private final StringReader stringReader;
029    
030      enum EndOfLineDelimiter {
031        LF, CR_PLUS_LF, CR
032      }
033    
034      public StringArrayReader(String[] lines) {
035        this(lines, EndOfLineDelimiter.LF);
036      }
037    
038      public StringArrayReader(String[] lines, EndOfLineDelimiter endOfLineDelimiter) {
039        if (lines == null) {
040          throw new IllegalStateException("lines object can't be null.");
041        }
042        String content = convertArrayToStringAndAppendEndOfLine(lines, endOfLineDelimiter);
043        stringReader = new StringReader(content);
044      }
045    
046      private String convertArrayToStringAndAppendEndOfLine(String[] lines, EndOfLineDelimiter endOfLineDelimiter) {
047        StringBuilder content = new StringBuilder();
048        for (int i = 0; i < lines.length; i++) {
049          content.append(lines[i]);
050          if (i != (lines.length - 1)) {
051            switch (endOfLineDelimiter) {
052              case LF:
053                content.append('\n');
054                break;
055              case CR:
056                content.append('\r');
057                break;
058              case CR_PLUS_LF:
059                content.append("\r\n");
060                break;
061            }
062          }
063        }
064        return content.toString();
065      }
066    
067      @Override
068      public void close() throws IOException {
069        stringReader.close();
070      }
071    
072      @Override
073      public boolean ready() throws IOException {
074        return stringReader.ready();
075      }
076    
077      @Override
078      public boolean markSupported() {
079        return stringReader.markSupported();
080      }
081    
082      @Override
083      public void mark(int readAheadLimit) throws IOException {
084        stringReader.mark(readAheadLimit);
085      }
086    
087      @Override
088      public void reset() throws IOException {
089        stringReader.reset();
090      }
091    
092      @Override
093      public int read(char[] cbuf, int off, int len) throws IOException {
094        return stringReader.read(cbuf, off, len);
095      }
096    }