1 30 package org.jruby.util; 31 32 import java.io.BufferedInputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.PushbackInputStream ; 36 37 38 42 public class RubyInputStream extends PushbackInputStream { 43 public RubyInputStream(InputStream in) { 44 super(new BufferedInputStream (in)); 45 } 46 47 public String gets(String separatorString) throws IOException { 48 if (separatorString == null) { 49 return getsEntireStream(); 50 } 51 final char[] separator = separatorString.toCharArray(); 52 53 int c = read(); 54 if (c == -1) { 55 return null; 56 } 57 58 StringBuffer buffer = new StringBuffer (); 59 60 LineLoop : while (true) { 61 while (c != separator[0] && c != -1) { 62 buffer.append((char) c); 63 c = read(); 64 } 65 for (int i = 0; i < separator.length; i++) { 66 if (c == -1) { 67 break LineLoop; 68 } else if (c != separator[i]) { 69 continue LineLoop; 70 } 71 buffer.append((char) c); 72 if (i < separator.length - 1) { 73 c = read(); 74 } 75 } 76 break; 77 } 78 return buffer.toString(); 79 } 80 81 private String getsEntireStream() throws IOException { 82 StringBuffer result = new StringBuffer (); 83 int c; 84 while ((c = read()) != -1) { 85 result.append((char) c); 86 } 87 return result.toString(); 88 } 89 } 90 | Popular Tags |