01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.util;
17:
18: import java.io.CharArrayWriter;
19: import java.nio.BufferOverflowException;
20: import java.nio.BufferUnderflowException;
21:
22: /**
23: * Utility class for directly modifying a character array.
24: */
25: public class StringCopier {
26: private final CharArrayWriter out = new CharArrayWriter();
27: private final char[] in;
28: private int inPos = 0;
29:
30: public StringCopier(char[] in) {
31: this .in = in;
32: }
33:
34: public void commit(char[] replaceWith, int startReplace,
35: int endReplace) {
36: if (startReplace < inPos) {
37: throw new BufferUnderflowException();
38: }
39: if (endReplace > in.length) {
40: throw new BufferOverflowException();
41: }
42:
43: // commit any characters up to the beginning of the replacement
44: out.write(in, inPos, startReplace - inPos);
45:
46: // commit the replacement
47: out.write(replaceWith, 0, replaceWith.length);
48:
49: // skip over the replaced characters
50: inPos = endReplace;
51: }
52:
53: public char[] finish() {
54: // commit any uncommitted characters
55: out.write(in, inPos, in.length - inPos);
56: inPos = in.length;
57: return out.toCharArray();
58: }
59: }
|