|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.editor.ext; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import org.netbeans.editor.Analyzer; /** * Management of storage of the data for the java completion. * * @author Miloslav Metelka, Martin Roskanin * @version 1.00 */ public class FileStorage { /** Constant for checking the maximum size of the string. * If the string size exceeds this value the error is thrown * as there's very likely corruption of the file. */ private static final int MAX_STRING = 60000; private static final int BYTES_INCREMENT = 2048; private static final byte[] EMPTY_BYTES = new byte[0]; Thread currentLock; protected boolean openedForWrite; public boolean fileNotFound = false; protected DataAccessor da; protected boolean opened = false; /** Current offset in the bytes array */ protected int offset; /** Byte array holding the data that were read from file */ protected byte[] bytes = EMPTY_BYTES; /** Shared char array to use for reading strings */ char[] chars = Analyzer.EMPTY_CHAR_ARRAY; /** String cache */ StringCache strCache; /** How many times current writer requested writing */ private int lockDeep; /** file unlock without previous file lock */ private static final String WRITE_LOCK_MISSING = "Unlock file without previous lock file"; // NOI18N /** Version of read database file */ private int version = 1; // set to default version /** 7th bit * 1 - more bytes were used for encoding of the int value * 0 - only one byte has been used. The int value is less than 128. */ private static final int BIT7 = (1 << 7); /** 5th and 6th bit * 6th | 5th * 0 | 0 - 1 byte will succed * 0 | 1 - 2 bytes will succed * 1 | 0 - 3 bytes will succed * 1 | 1 - 4 bytes will succed */ private static final int BIT6 = (1 << 6); private static final int BIT5 = (1 << 5); /** @param fileName name of file to operate over */ public FileStorage(String fileName) { this(fileName, new StringCache()); } public FileStorage(String fileName, StringCache strCache) { da = new FileAccessor(new File(fileName)); this.strCache = strCache; } public FileStorage(DataAccessor da, StringCache strCache){ this.da = da; this.strCache = strCache; } /** Setter for version of Code Completion DB file. */ public void setVersion(int ver){ version = ver; } public void open(boolean requestWrite) throws IOException { if (openedForWrite == requestWrite) { ensureOpen(requestWrite); da.seek(getFileLength()); return; // already opened with correct type } else { // opened with different type close(); } // open the file ensureOpen(requestWrite); da.seek(getFileLength()); openedForWrite = requestWrite; offset = 0; } private void ensureOpen(boolean requestWrite) throws IOException { if (!opened) { da.open(requestWrite); opened = true; } } public void close() throws IOException { opened = false; da.close(); } /** Check size of bytes[] array */ protected void checkBytesSize(int len) { if (bytes.length < len) { byte[] newBytes = new byte[len + BYTES_INCREMENT]; System.arraycopy(bytes, 0, newBytes, 0, bytes.length); bytes = newBytes; } } /** Read some part of the file into the begining of bytes array * and reset offset to zero. */ public void read(int len) throws IOException { checkBytesSize(len); da.read(bytes, 0, len); offset = 0; } /** Write bytes array (with offset length) to the file */ public void write() throws IOException { if (offset > 0) { da.append(bytes, 0, offset); } offset = 0; } public void seek(int filePointer) throws IOException { da.seek(filePointer); } public String getFileName() { return da.toString(); } public int getFilePointer() throws IOException { return (int)da.getFilePointer(); } public void setOffset(int offset) { this.offset = offset; } public int getOffset() { return offset; } public int getFileLength() throws IOException { return da.getFileLength(); } public void resetBytes() { bytes = EMPTY_BYTES; } /** Reset the size of the file and set current offset to zero. */ public void resetFile() throws IOException { open(true); offset = 0; da.resetFile(); close(); } /** Get the integer value from the bytes[] array */ public int getInteger() { if (version == 1){ int i = bytes[offset++]; i = (i << 8) + (bytes[offset++] & 255); i = (i << 8) + (bytes[offset++] & 255); i = (i << 8) + (bytes[offset++] & 255); return i; } if (version == 2){ return decodeInteger(); } return 0; } /** Get the string value from the bytes[] array */ public String getString() { int len = getInteger(); // length of string if (len < 0) { throw new RuntimeException("Consistency error: read string length=" + len); // NOI18N } if (len > MAX_STRING) { throw new RuntimeException("FileStorage: String len is " + len // NOI18N + ". There's probably a corruption in the file '" // NOI18N + getFileName() + "'."); // NOI18N } if(version == 1){ if (chars.length < len) { // check chars array size chars = new char[2 * len]; } for (int i = 0; i < len; i++) { chars[i] = (char)((bytes[offset] << 8) + (bytes[offset + 1] & 255)); offset += 2; } String s = null; if (len >= 0) { if (strCache != null) { s = strCache.getString(chars, 0, len); } else { // no string cache s = new String(chars, 0, len); } } return s; }else if (version == 2){ try{ String s = new String(bytes,offset,len,getEncoding()); offset += len; return s; }catch(java.io.UnsupportedEncodingException e){ e.printStackTrace(); return ""; } catch(ArrayIndexOutOfBoundsException ex){ StringBuffer sb = new StringBuffer(len); for (int i=0;i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.