001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.io.IOException;
017: import java.io.Writer;
018:
019: import javax.swing.text.BadLocationException;
020:
021: /**
022: * DocCacheSupport is a base class for caching document which is got by some
023: * reader and enable its editing.
024: *
025: * @author Miloslav Metelka
026: * @version 1.00
027: */
028:
029: abstract class DocCacheSupport {
030:
031: private static final int SAVE_BUFFER_LEN = 16384;
032:
033: /** Current document length in characters */
034: protected int docLen;
035:
036: /** Count of characters read */
037: long statCharsRead;
038: /** Count of characters written */
039: long statCharsWritten;
040:
041: /**
042: * Reading from the temporary storage into destination buffer.
043: *
044: * @param pos
045: * position in temp storage
046: * @param buffer
047: * target storage
048: * @param offset
049: * offset in buffer
050: * @param len
051: * length of data in buffer to write
052: */
053: public abstract void read(int pos, char buffer[], int offset,
054: int len) throws BadLocationException;
055:
056: /**
057: * Write buffer at the specified position. Rewrites part of the document. It
058: * can write beyond the end of the document, however the initial position
059: * must be inside or at the end of the document.
060: *
061: * @param pos
062: * position in temp storage
063: * @param buffer
064: * buffer with source data
065: * @param offset
066: * offset of data in buffer
067: * @param len
068: * length of data in buffer
069: */
070: public abstract void write(int pos, char buffer[], int offset,
071: int len) throws BadLocationException;
072:
073: /**
074: * Insert buffer into the document at specified position.
075: *
076: * @param pos
077: * insertion position in temp storage document
078: * @param buffer
079: * source buffer with insertion data
080: * @param offset
081: * offset of data in the buffer
082: * @param len
083: * length of data in buffer
084: */
085: public abstract void insert(int pos, char buffer[], int offset,
086: int len) throws BadLocationException;
087:
088: /**
089: * Insert string into the document at specified position.
090: *
091: * @param pos
092: * insertion position in temp storage document
093: * @param text
094: * text to insert
095: * @param offset
096: * offset of data in the buffer
097: * @param len
098: * length of data in buffer
099: */
100: public abstract void insertString(int pos, String text, int offset,
101: int len) throws BadLocationException;
102:
103: /**
104: * Delete a specified count of chars at specified position.
105: *
106: * @param pos
107: * position of removal in temp storage
108: * @param len
109: * length of data to remove
110: */
111: public abstract void remove(int pos, int len)
112: throws BadLocationException;
113:
114: /**
115: * Helps to do less allocations if the initial file size is known. By
116: * default it's not implemented.
117: *
118: * @param capacity
119: * space that will be preallocated
120: */
121: public void ensureCapacity(int capacity) {
122: }
123:
124: /**
125: * Saving the tepmorary document to a writer.
126: *
127: * @param writer
128: * writer where to write data from temp storage
129: */
130: public void save(Writer writer) throws IOException {
131: char saveBuffer[] = new char[SAVE_BUFFER_LEN];
132: int actPos = 0, len;
133: while (actPos < docLen) {
134: len = Math.min(docLen - actPos, SAVE_BUFFER_LEN);
135: try {
136: read(actPos, saveBuffer, 0, len);
137: } catch (BadLocationException e) {
138: if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
139: e.printStackTrace();
140: }
141: }
142: writer.write(saveBuffer, 0, len);
143: actPos += len;
144: }
145: writer.flush(); // flush the stream at the end
146: }
147:
148: /**
149: * Destroy the CacheSupport (delete temp file etc.)
150: */
151: public void destroy() {
152: }
153:
154: /** Getter: Document length */
155: public final int getDocLength() {
156: return docLen;
157: }
158:
159: public boolean supportsDirectMode() {
160: return false;
161: }
162:
163: public char[] getDirectModeBuffer() {
164: return null;
165: }
166:
167: }
|