~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/UnsynchronizedBufferedWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002-2005 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@metalab.unc.edu. 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
    
 
34
    public UnsynchronizedBufferedWriter(Writer out) {
 
35
        this.out = out;
 
36
    }
 
37
 
 
38
    
 
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
    
 
44
    public void write(String s) throws IOException {
 
45
         write(s, 0, s.length());
 
46
    }
 
47
 
 
48
    
 
49
    public void write(String s, int offset, int length) throws IOException {
 
50
    
 
51
        while (length > 0) {
 
52
            int n = Math.min(CAPACITY - position, length);
 
53
            s.getChars(offset, offset + n, buffer, position);
 
54
            position += n;
 
55
            offset += n;
 
56
            length -= n;
 
57
            if (position >= CAPACITY) flushInternal();
 
58
        }
 
59
        
 
60
    }
 
61
        
 
62
    
 
63
    public void write(int c) throws IOException {
 
64
        if (position >= CAPACITY) flushInternal();
 
65
        buffer[position] = (char) c;
 
66
        position++;
 
67
    }
 
68
 
 
69
    
 
70
    public void flush() throws IOException {
 
71
        flushInternal();
 
72
        out.flush();
 
73
    }
 
74
 
 
75
 
 
76
    private void flushInternal() throws IOException {
 
77
        if (position != 0) {
 
78
            out.write(buffer, 0, position);
 
79
            position = 0;
 
80
        }
 
81
    }
 
82
 
 
83
    
 
84
    public void close() throws IOException {
 
85
        throw new UnsupportedOperationException("How;d we get here?");
 
86
    }
 
87
 
 
88
}