1 | /* |
---|
2 | * SeekableStringWriter.java |
---|
3 | * |
---|
4 | * Copyright (C) 2016 Olof-Joachim Frahm |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | import java.io.Writer; |
---|
39 | import java.text.MessageFormat; |
---|
40 | |
---|
41 | public final class SeekableStringWriter extends Writer { |
---|
42 | private final StringBuffer stringBuffer; |
---|
43 | private int offset = 0; |
---|
44 | |
---|
45 | public SeekableStringWriter() { |
---|
46 | stringBuffer = new StringBuffer(); |
---|
47 | } |
---|
48 | |
---|
49 | public SeekableStringWriter(int initialSize) { |
---|
50 | stringBuffer = new StringBuffer(initialSize); |
---|
51 | } |
---|
52 | |
---|
53 | public SeekableStringWriter append(char c) { |
---|
54 | write(c); |
---|
55 | return this; |
---|
56 | } |
---|
57 | |
---|
58 | public SeekableStringWriter append(CharSequence csq) { |
---|
59 | write(csq.toString()); |
---|
60 | return this; |
---|
61 | } |
---|
62 | |
---|
63 | public SeekableStringWriter append(CharSequence csq, int start, int end) { |
---|
64 | write(csq.subSequence(start, end).toString()); |
---|
65 | return this; |
---|
66 | } |
---|
67 | |
---|
68 | @Override |
---|
69 | public void write(char[] cbuf) { |
---|
70 | _write(cbuf, 0, cbuf.length); |
---|
71 | } |
---|
72 | |
---|
73 | @Override |
---|
74 | public void write(char[] cbuf, int off, int len) { |
---|
75 | int bufLen = cbuf.length; |
---|
76 | |
---|
77 | if (off < 0 || off > bufLen || len < 0 || off + len > bufLen) |
---|
78 | throw new IllegalArgumentException(); |
---|
79 | |
---|
80 | _write(cbuf, off, len); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public void write(int c) { |
---|
85 | try { |
---|
86 | if (offset == stringBuffer.length()) |
---|
87 | stringBuffer.append((char) c); |
---|
88 | else |
---|
89 | stringBuffer.setCharAt(offset, (char) c); |
---|
90 | ++offset; |
---|
91 | } catch (IndexOutOfBoundsException e) { |
---|
92 | error(new JavaException(e)); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | public void write(String str) { |
---|
98 | write(str, 0, str.length()); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void write(String str, int off, int len) { |
---|
103 | write(str.toCharArray(), off, len); |
---|
104 | } |
---|
105 | |
---|
106 | private void _write(char[] cbuf, int off, int len) { |
---|
107 | int strLen = stringBuffer.length(); |
---|
108 | int space = strLen - offset; |
---|
109 | |
---|
110 | int written = Math.min(len, space); |
---|
111 | |
---|
112 | if (written > 0) |
---|
113 | stringBuffer.replace(offset, offset + written, new String(cbuf, off, written)); |
---|
114 | |
---|
115 | if (written < len) |
---|
116 | stringBuffer.append(cbuf, off + written, len - written); |
---|
117 | |
---|
118 | offset += len; |
---|
119 | } |
---|
120 | |
---|
121 | public void seek(int offset) { |
---|
122 | if (offset < 0 || offset > stringBuffer.length()) |
---|
123 | throw new IllegalArgumentException(); |
---|
124 | this.offset = offset; |
---|
125 | } |
---|
126 | |
---|
127 | public StringBuffer getBuffer() { |
---|
128 | return stringBuffer; |
---|
129 | } |
---|
130 | |
---|
131 | public int getOffset() { |
---|
132 | return offset; |
---|
133 | } |
---|
134 | |
---|
135 | @Override |
---|
136 | public String toString() { |
---|
137 | return stringBuffer.toString(); |
---|
138 | } |
---|
139 | |
---|
140 | @Override |
---|
141 | public void close() {} |
---|
142 | |
---|
143 | @Override |
---|
144 | public void flush() {} |
---|
145 | |
---|
146 | public String toStringAndClear() { |
---|
147 | String result = stringBuffer.toString(); |
---|
148 | stringBuffer.delete(0, stringBuffer.length()); |
---|
149 | offset = 0; |
---|
150 | return result; |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|