1 /* Copyright 2002-2006 Elliotte Rusty Harold 2 3 This library is free software; you can redistribute it and/or modify 4 it under the terms of version 2.1 of the GNU Lesser General Public 5 License as published by the Free Software Foundation. 6 7 This library is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU Lesser General Public License for more details. 11 12 You should have received a copy of the GNU Lesser General Public 13 License along with this library; if not, write to the 14 Free Software Foundation, Inc., 59 Temple Place, Suite 330, 15 Boston, MA 02111-1307 USA 16 17 You can contact Elliotte Rusty Harold by sending e-mail to 18 elharo@ibiblio.org. Please include the word "XOM" in the 19 subject line. The XOM home page is located at http://www.xom.nu/ 20 */ 21 package nu.xom; 22 23 import java.io.IOException; 24 import java.io.Writer; 25 26 final class UnsynchronizedBufferedWriter extends Writer { 27 28 private final static int CAPACITY = 8192; 29 private char[] buffer = new char[CAPACITY]; 30 private int position = 0; 31 private Writer out; 32 33 UnsynchronizedBufferedWriter(Writer out)34 public UnsynchronizedBufferedWriter(Writer out) { 35 this.out = out; 36 } 37 38 write(char[] buffer, int offset, int length)39 public void write(char[] buffer, int offset, int length) throws IOException { 40 throw new UnsupportedOperationException("XOM bug: this statement shouldn't be reachable."); 41 } 42 43 write(String s)44 public void write(String s) throws IOException { 45 write(s, 0, s.length()); 46 } 47 48 write(String s, int offset, int length)49 public void write(String s, int offset, int length) throws IOException { 50 51 while (length > 0) { 52 int n = CAPACITY - position; 53 if (length < n) n = length; 54 s.getChars(offset, offset + n, buffer, position); 55 position += n; 56 offset += n; 57 length -= n; 58 if (position >= CAPACITY) flushInternal(); 59 } 60 61 } 62 63 write(int c)64 public void write(int c) throws IOException { 65 if (position >= CAPACITY) flushInternal(); 66 buffer[position] = (char) c; 67 position++; 68 } 69 70 flush()71 public void flush() throws IOException { 72 flushInternal(); 73 out.flush(); 74 } 75 76 flushInternal()77 private void flushInternal() throws IOException { 78 if (position != 0) { 79 out.write(buffer, 0, position); 80 position = 0; 81 } 82 } 83 84 close()85 public void close() throws IOException { 86 throw new UnsupportedOperationException("How'd we get here?"); 87 } 88 89 } 90