001package org.unix4j.io; 002 003import java.util.LinkedList; 004 005import org.unix4j.line.Line; 006import org.unix4j.util.StringUtil; 007 008/** 009 * Input device reading the input from a string. If the string contains 010 * line-ending code (UNIX or DOS), it is split into multiple lines. 011 */ 012public class StringInput extends BufferedInput { 013 014 /** 015 * Constructor with lines. Each line is tested for new line characters and 016 * possibly split into multiple lines. 017 * 018 * @param lines 019 * the lines for this input 020 */ 021 public StringInput(String... lines) { 022 super(toList(lines)); 023 } 024 025 /** 026 * Constructor with lines. Each line is tested for new line characters and 027 * possibly split into multiple lines. 028 * 029 * @param lines 030 * the lines for this input 031 */ 032 public StringInput(Iterable<? extends String> lines) { 033 super(toList(lines)); 034 } 035 036 private static LinkedList<Line> toList(String[] lines) { 037 final LinkedList<Line> list = new LinkedList<Line>(); 038 for (int i = 0; i < lines.length; i++) { 039 list.addAll(StringUtil.splitLines(lines[i])); 040 } 041 return list; 042 } 043 044 private static LinkedList<Line> toList(Iterable<? extends String> lines) { 045 final LinkedList<Line> list = new LinkedList<Line>(); 046 for (String line : lines) { 047 list.addAll(StringUtil.splitLines(line)); 048 } 049 return list; 050 } 051}