001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: TextLineWriter.java$
021: * $FileID: 4487$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:20 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.server.source;
032:
033: /**
034: * Title: SourceJammer
035: * Description: Server code for SourceJammer Java open-source source control project.
036: * Copyright: Copyright (c) 2001
037: * Company: Robert MacGrogan
038: * @author Robert MacGrogan
039: * @version 1.0
040: */
041:
042: import org.sourcejammer.util.AppConfig;
043: import org.sourcejammer.server.make.EndOfSourceException;
044:
045: /**
046: * @deprecated This clas is deprecated. Only kept in here in case conversion
047: * from old text file format is required.
048: *
049: * This class is used to append lines into a char array, using a specified
050: * end of line char array (typically linefeed or carriage return / linefeed.
051: *
052: * The lines passed into this class should not include the end of line
053: * characters. If the end of line characters are included, these characters
054: * will be doubled in the final output.
055: */
056: public class TextLineWriter implements TextLineAdder,
057: java.io.Serializable {
058:
059: private char[] mcaText;
060: private int miIndex;
061: private char[] mcaEndOfLine;
062:
063: private static final int DEFAULT_BUFFER_START_SIZE = 10000;
064:
065: /**
066: * Default constructor. Creates a TextLineWriter with default
067: * buffer size and default end of line characters.
068: */
069: public TextLineWriter() {
070: this (DEFAULT_BUFFER_START_SIZE);
071: }
072:
073: /**
074: * Constructs a TextLineWriter with the passed-in starting buffer size
075: * and the default end of line characters.<br /><br />
076: *
077: * It is important to use an accurate starting buffer size. The buffer
078: * will resize automatically if the output grows larger than the starting
079: * size, but the process of resizing the buffer is fairly inefficient.
080: *
081: * @param size - starting size of the buffer (number of characters
082: * expected in the final output.
083: */
084: public TextLineWriter(int size) {
085: this (size, AppConfig.getInstance().getDefaultEndOfLine());
086: }
087:
088: /**
089: * Constructs a TextLineWriter with the passed-in end of line characters
090: * using the default buffer size.
091: *
092: * @param endOfLine -- a character array containing the end of line characters
093: * to use for the output of this TextLineWriter.
094: */
095: public TextLineWriter(char[] endOfLine) {
096: this (DEFAULT_BUFFER_START_SIZE, endOfLine);
097: }
098:
099: /**
100: * Constructs a TextLineWriter with the passed-in starting buffer size
101: * and end of line characters.
102: *
103: * @param size - starting size of the buffer (number of characters
104: * expected in the final output.
105: * @param endOfLine -- a character array containing the end of line characters
106: * to use for the output of this TextLineWriter.
107: */
108: public TextLineWriter(int size, char[] endOfLine) {
109: mcaText = new char[size];
110: mcaEndOfLine = endOfLine;
111: miIndex = 0;
112: }
113:
114: /**
115: * Adds a line to this TextLineWriter. This class will append the end of line
116: * characters to the end of the line.
117: *
118: * @param line -- a line of text as a char array.
119: */
120: public void writeLine(char[] line) {
121: if (miIndex + (line.length + mcaEndOfLine.length) >= mcaText.length) {
122: redimCharArray();
123: }
124: for (int i = 0; i < line.length; i++) {
125: mcaText[miIndex] = line[i];
126: miIndex++;
127: }
128: //System.out.print("xx " + String.valueOf(line));
129: for (int i = 0; i < mcaEndOfLine.length; i++) {
130: //System.out.print("EOL char.");
131: mcaText[miIndex] = mcaEndOfLine[i];
132: miIndex++;
133: }
134: //System.out.println();
135: }
136:
137: /**
138: * Adds a line to this TextLineWriter. This class will append the end of line
139: * characters to the end of the line.
140: *
141: * @param line -- a line of text as a String.
142: */
143: public void writeLine(String sLine) {
144: char[] line = sLine.toCharArray();
145: writeLine(line);
146: }
147:
148: /**
149: * Writes the entire contents of the TextLineIterator to this TextLineWriter,
150: * beginning with the current line in the iterator.
151: *
152: * @param oIterator -- a TextLineIterator.
153: */
154: public void write(TextLineIterator oIterator) {
155: try {
156: while (oIterator.hasMoreLines()) {
157: writeLine(oIterator.getNextLine());
158: }
159: } catch (EndOfSourceException ex) {
160: //ignore. Just Stop adding lines.
161: }
162: }
163:
164: /**
165: * Writes the contents of this TextLineWriter to a char array. The char array
166: * includes the end of line characters.
167: */
168: public char[] toCharArray() {
169: //This ensures we don't return a char array with a bunch of empty chars.
170: //And that a superfluous eol char is not appended to end of file.
171: int iReturnSize = miIndex - mcaEndOfLine.length;
172: char[] caReturn = new char[iReturnSize];
173: for (int i = 0; i < iReturnSize; i++) {
174: caReturn[i] = mcaText[i];
175: }
176: return caReturn;
177: }
178:
179: /**
180: * Resizes the buffer for this TextLineWriter.<br /><br />
181: *
182: * Avoid calling this method if possible. It will be pretty slow.
183: */
184: private void redimCharArray() {
185: if (mcaText.length * 10 >= Integer.MAX_VALUE) {
186: //throw exception
187: }
188: int iNewSize = mcaText.length * 10;
189: char[] caBuf = new char[iNewSize];
190: for (int i = 0; i < mcaText.length; i++) {
191: caBuf[i] = mcaText[i];
192: }
193: mcaText = caBuf;
194: }
195:
196: }
|