/** Creates a new instance of ByteArrayString */ public starIndex(FileAccessBase indexFile, starDict dict) { super(dict); if (Runtime.getRuntime().totalMemory() < 1024 * 1024) bufLen = 1024 * 8; FileAccessBase file = null; try { this.dict = dict; this.indexFile = indexFile; this.fileName = indexFile.getAbsolutePath(); boolean indexOfIndexLoaded = false; file = FileFactory.openFileAccess(fileName + ".idx", true); try { if (file != null && file.isFile() && file.canRead()) { if (file.fileSize() > 0) { loadFromIndexOfIndexFile(file); indexOfIndexLoaded = true; } } } catch (IOException ex) { ex.printStackTrace(); } finally { if (file != null) { file.close(); file = null; } } if (!indexOfIndexLoaded) { try { DictManager.getInstance().NotifyPromptStart("tuningindex"); file = getIndexFile(); loadFromIndexFile(file); // System.gc(); saveToIndexOfIndexFile(fileName + ".idx"); DictManager.getInstance().NotifyPromptEnd("tuningindex"); } catch (Exception ex) { // System.out.println("Error While load index:" + ex.getMessage()); ex.printStackTrace(); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (file != null) { file.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
private synchronized void saveToIndexOfIndexFile(String fileName) throws IOException { FileAccessBase file = FileFactory.newFileAccess(fileName); if (file == null) { return; } if (!file.exists()) { file.create(); } if (file.isFile() && file.canWrite()) { int writeBufLen = 256 + 1 + 16; byte[] buf = new byte[bufLen]; int pos = 0; int ret = 0; int contentLength = 0; starIndex.putIntintoByte(this.bucketSize, 0, buf); // System.out.println("write " + wordList.size()); file.write(buf, 0, 4); for (int i = 0; i < wordList.size(); i++) { starIndexItem item = (starIndexItem) wordList.elementAt(i); if (item.getBytes().length + 19 + pos < bufLen) { ByteArrayString.byteCopy(item.getBytes(), 0, buf, pos, item.getBytes().length); pos += item.getBytes().length; buf[pos] = 0; pos++; starIndex.putIntintoByte(item.getbucket(0).getStart(), pos, buf); pos += 4; starIndex.putIntintoByte(item.getbucket(0).getLength(), pos, buf); pos += 4; starIndex.putIntintoByte(item.getStart(), pos, buf); pos += 4; starIndex.putIntintoByte(item.getLength(), pos, buf); pos += 4; } else { file.write(buf, 0, pos); pos = 0; i--; } } if (pos != 0) { file.write(buf, 0, pos); } file.flush(); file.close(); buf = null; } }
private void loadFromIndexFile(FileAccessBase file) throws IOException { if (file != null && file.isFile() && file.canRead()) { byte[] buf = new byte[bufLen]; int pos = 0; int ret = 0; int contentLength = 0; do { ret = file.read(buf, pos, buf.length - pos); if (ret <= 0) { break; } contentLength = pos + ret; pos = parseBuf(buf, contentLength); // System.out.println( Runtime.getRuntime().freeMemory() + " of " + // Runtime.getRuntime().totalMemory() ); } while (true); file.close(); buf = null; // System.gc(); } }
private void loadFromIndexOfIndexFile(final FileAccessBase file) throws IOException { if (file.isFile() && file.canRead()) { byte[] buf = new byte[bufLen]; int pos = 0; int ret = 0; int contentLength = 0; // read the bocket size,it's 4 byte ret = file.read(buf, pos, 4); bucketSize = getIntFromByte(pos, buf); do { ret = file.read(buf, pos, buf.length - pos); if (ret == -1) { break; } contentLength = pos + ret; pos = parseIndexOfIndexBuf(buf, contentLength); } while (true); file.close(); buf = null; // System.gc(); } }