1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 44 45 public class LineInputStream extends FilterInputStream { 46 47 private char[] lineBuffer = null; 49 public LineInputStream(InputStream in) { 50 super(in); 51 } 52 53 64 public String readLine() throws IOException { 65 InputStream in = this.in; 66 char[] buf = lineBuffer; 67 68 if (buf == null) 69 buf = lineBuffer = new char[128]; 70 71 int c1; 72 int room = buf.length; 73 int offset = 0; 74 75 while ((c1 = in.read()) != -1) { 76 if (c1 == '\n') break; 78 else if (c1 == '\r') { 79 int c2 = in.read(); 81 if (c2 == '\r') c2 = in.read(); 83 if (c2 != '\n') { 84 if (!(in instanceof PushbackInputStream)) 86 in = this.in = new PushbackInputStream(in); 87 ((PushbackInputStream)in).unread(c2); 88 } 89 break; } 91 92 if (--room < 0) { buf = new char[offset + 128]; 96 room = buf.length - offset - 1; 97 System.arraycopy(lineBuffer, 0, buf, 0, offset); 98 lineBuffer = buf; 99 } 100 buf[offset++] = (char)c1; 101 } 102 103 if ((c1 == -1) && (offset == 0)) 104 return null; 105 106 return String.copyValueOf(buf, 0, offset); 107 } 108 } 109 | Popular Tags |