1 19 20 package org.netbeans.modules.editor.guards; 21 22 import java.io.CharArrayWriter ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.UnsupportedEncodingException ; 27 import java.io.Writer ; 28 import java.util.List ; 29 import org.netbeans.api.editor.guards.GuardedSection; 30 import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider; 31 32 34 final class GuardedWriter extends Writer { 35 36 private Writer writer; 37 38 private CharArrayWriter buffer; 39 40 private final AbstractGuardedSectionsProvider gw; 41 42 private boolean isClosed = false; 43 44 private final List <GuardedSection> sections; 45 46 47 51 public GuardedWriter(AbstractGuardedSectionsProvider gw, OutputStream os, List <GuardedSection> list, String encoding) throws UnsupportedEncodingException { 52 if (encoding == null) 53 writer = new OutputStreamWriter (os); 54 else 55 writer = new OutputStreamWriter (os, encoding); 56 this.gw = gw; 57 sections = list; 58 } 59 60 61 public void write(char[] cbuf, int off, int len) throws IOException { 62 63 if (buffer == null) { 64 buffer = new CharArrayWriter (10240); 65 } 66 67 buffer.write(cbuf, off, len); 68 } 69 70 71 public void close() throws IOException { 72 if (isClosed) { 73 return; 74 } 75 isClosed = true; 76 if (buffer != null) { 77 char[] content = this.gw.writeSections(sections, buffer.toCharArray()); 78 writer.write(content); 79 } 80 writer.close(); 81 } 82 83 84 public void flush() throws IOException { 85 } 86 87 } 88 | Popular Tags |