1 21 24 package org.lobobrowser.html.io; 25 26 import java.io.*; 27 28 public class WritableLineReader extends LineNumberReader { 29 private final Reader delegate; 30 31 public WritableLineReader(Reader reader, int bufferSize) { 32 super(reader, bufferSize); 33 this.delegate = reader; 34 } 35 36 public WritableLineReader(Reader reader) { 37 super(reader); 38 this.delegate = reader; 39 } 40 41 44 public int read() throws IOException { 45 StringBuffer sb = this.writeBuffer; 46 if(sb != null && sb.length() > 0) { 47 char ch = sb.charAt(0); 48 sb.deleteCharAt(0); 49 if(sb.length() == 0) { 50 this.writeBuffer = null; 51 } 52 return (int) ch; 53 } 54 return super.read(); 55 } 56 57 61 public int read(char[] b, int off, int len) throws IOException { 62 StringBuffer sb = this.writeBuffer; 63 if(sb != null && sb.length() > 0) { 64 int srcEnd = Math.min(sb.length(), len); 65 sb.getChars(0, srcEnd, b, off); 66 sb.delete(0, srcEnd); 67 if(sb.length() == 0) { 68 this.writeBuffer = null; 69 } 70 return srcEnd; 71 } 72 return super.read(b, off, len); 73 } 74 75 public boolean ready() throws IOException { 76 StringBuffer sb = this.writeBuffer; 77 if(sb != null && sb.length() > 0) { 78 return true; 79 } 80 return super.ready(); 81 } 82 83 87 public void close() throws IOException { 88 this.writeBuffer = null; 89 super.close(); 90 } 91 92 private StringBuffer writeBuffer = null; 93 94 99 public void write(String text) throws IOException { 100 StringBuffer sb = this.writeBuffer; 102 if(sb == null) { 103 sb = new StringBuffer (); 104 this.writeBuffer = sb; 105 } 106 sb.append(text); 107 } 108 } 109 | Popular Tags |