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: /**
017: * Manages in-memory character cache to store contents of the document.
018: *
019: * @author Miloslav Metelka
020: * @version 1.00
021: */
022:
023: class MemCacheSupport extends DocCacheSupport {
024:
025: /** Increment for extending the cache */
026: private static final int CACHE_INCREMENT = 2048;
027:
028: /** Memory cache array */
029: char buffer[] = Analyzer.EMPTY_CHAR_ARRAY;
030:
031: /** Construct new memory cacheSupport with zero size */
032: public MemCacheSupport() {
033: }
034:
035: /**
036: * Construct new memory cacheSupport. Initialize the memory array.
037: *
038: * @param ensureCapacity
039: * how much space preallocate
040: */
041: public MemCacheSupport(int ensureCapacity) {
042: buffer = new char[ensureCapacity];
043: }
044:
045: /**
046: * check if there's enough space in the cache
047: *
048: * @param reqLen
049: * how many chars has to be available
050: */
051: private void checkSpace(int reqLen) {
052: if (buffer.length < reqLen) {
053: char tempCache[] = new char[reqLen + CACHE_INCREMENT
054: + reqLen / 8];
055: System.arraycopy(buffer, 0, tempCache, 0, getDocLength());
056: buffer = tempCache;
057: }
058: }
059:
060: public void ensureCapacity(int capacity) {
061: checkSpace(capacity);
062: }
063:
064: public void read(int pos, char cache[], int offset, int len) {
065: if (len == 0) {
066: return; // immediate return for void operations
067: }
068: System.arraycopy(buffer, pos, cache, offset, len); // copy part of the
069: // cache
070: }
071:
072: public void write(int pos, char cache[], int offset, int len) {
073: if (len == 0) {
074: return; // immediate return for void operations
075: }
076: checkSpace(pos + len);
077: System.arraycopy(cache, offset, buffer, pos, len);
078: docLen = Math.max(docLen, pos + len);
079: }
080:
081: public void insert(int pos, char cache[], int offset, int len) {
082: if (len == 0) {
083: return; // immediate return for void operations
084: }
085: checkSpace(getDocLength() + len);
086: System.arraycopy(buffer, pos, buffer, pos + len, getDocLength()
087: - pos);
088: System.arraycopy(cache, offset, buffer, pos, len);
089: docLen += len;
090: }
091:
092: public void insertString(int pos, String text, int offset, int len) {
093: if (len == 0) {
094: return; // immediate return for void operations
095: }
096: checkSpace(getDocLength() + len);
097: System.arraycopy(buffer, pos, buffer, pos + len, getDocLength()
098: - pos);
099: text.getChars(offset, offset + len, buffer, pos);
100: docLen += len;
101: }
102:
103: public void remove(int pos, int len) {
104: if (len == 0) {
105: return; // immediate return for void operations
106: }
107: System.arraycopy(buffer, pos + len, buffer, pos, getDocLength()
108: - (pos + len));
109: docLen -= len;
110: }
111:
112: public boolean supportsDirectMode() {
113: return true;
114: }
115:
116: public char[] getDirectModeBuffer() {
117: return buffer;
118: }
119:
120: }
|