KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > StringCopier


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.nio.BufferOverflowException JavaDoc;
20 import java.nio.BufferUnderflowException JavaDoc;
21
22 /**
23  * Utility class for directly modifying a character array.
24  */

25 public class StringCopier {
26   private final CharArrayWriter JavaDoc out = new CharArrayWriter JavaDoc();
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, int endReplace) {
35     if (startReplace < inPos) {
36       throw new BufferUnderflowException JavaDoc();
37     }
38     if (endReplace > in.length) {
39       throw new BufferOverflowException JavaDoc();
40     }
41
42     // commit any characters up to the beginning of the replacement
43
out.write(in, inPos, startReplace - inPos);
44
45     // commit the replacement
46
out.write(replaceWith, 0, replaceWith.length);
47
48     // skip over the replaced characters
49
inPos = endReplace;
50   }
51
52   public char[] finish() {
53     // commit any uncommitted characters
54
out.write(in, inPos, in.length - inPos);
55     inPos = in.length;
56     return out.toCharArray();
57   }
58 }
59
Popular Tags