@Override public FileStorage openFile(String name, String mode, boolean mustExist) { if (mustExist && !FileUtils.exists(name)) { throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); } FileStorage fileStorage; if (cipher == null) { fileStorage = FileStorage.open(this, name, mode); } else { fileStorage = FileStorage.open(this, name, mode, cipher, fileEncryptionKey, 0); } fileStorage.setCheckedWriting(false); try { fileStorage.init(); } catch (DbException e) { fileStorage.closeSilently(); throw e; } return fileStorage; }
private FileStorage getFileStorage(int chunkId) { String chunkFileName = btreeStoragePath + File.separator + chunkId + AOStorage.SUFFIX_AO_FILE; FileStorage fileStorage = new FileStorage(); fileStorage.open(chunkFileName, map.config); return fileStorage; }
byte[] salt; byte[] magic = HEADER.getBytes(); if (length() < HEADER_LENGTH) { writeDirect(magic, 0, len); salt = generateSalt(); writeDirect(salt, 0, len); initKey(salt); write(magic, 0, len); checkedWriting = true; } else { seek(0); byte[] buff = new byte[len]; readFullyDirect(buff, 0, len); if (!Arrays.equals(buff, magic)) { throw DbException.get(ErrorCode.FILE_VERSION_ERROR_1, name); readFullyDirect(salt, 0, len); initKey(salt); readFully(buff, 0, Constants.FILE_BLOCK_SIZE); if (!Arrays.equals(buff, magic)) { throw DbException.get(ErrorCode.FILE_ENCRYPTION_ERROR_1, name);
@Override public void done() { file.seek(FileStorage.HEADER_LENGTH); file.autoDelete(); }
/** * Close the result list and delete the temporary file. */ public void close() { if (file != null) { file.autoDelete(); file.closeAndDeleteSilently(); file = null; rowBuff = null; } }
@Override public FileStorage openFile(String name, String openMode, boolean mustExist) { if (mustExist && !FileUtils.exists(name)) { throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); } FileStorage fileStorage = FileStorage.open(this, name, openMode, dbSettings.cipher, dbSettings.filePasswordHash); try { fileStorage.init(); } catch (DbException e) { fileStorage.closeSilently(); throw e; } return fileStorage; }
private void initStore() { Database db = session.getDatabase(); byte[] key = null; if (cipher != null && password != null) { char[] pass = password.optimize(session).getValue(session).getString().toCharArray(); key = SHA256.getKeyPasswordHash("script", pass); } String file = getFileName(); fileStorage = FileStorage.open(db, file, "rw", cipher, key); fileStorage.setCheckedWriting(false); fileStorage.init(); }
RedoLogChunk(int id, Map<String, String> config) { this.id = id; this.config = config; String chunkFileName = getChunkFileName(config, id); fileStorage = new FileStorage(); fileStorage.open(chunkFileName, config); queue = new LinkedTransferQueue<>(); pos = fileStorage.size(); if (pos > 0) read(); }
/** * Read a number of bytes without decrypting. * * @param b the target buffer * @param off the offset * @param len the number of bytes to read */ protected void readFullyDirect(byte[] b, int off, int len) { readFully(b, off, len); }
fileStorage.openFile(); if (fileStorage.length() == fileStorage.getFilePointer()) { close(); return; fileStorage.readFully(page.getBytes(), 0, Constants.FILE_BLOCK_SIZE); page.reset(); remainingInBuffer = page.getInt(); page.reset(); page.getInt(); fileStorage.readFully(page.getBytes(), Constants.FILE_BLOCK_SIZE, len); page.reset(); page.getInt(); fileStorage.closeFile();
/** * Open a non encrypted file store with the given settings. * * @param handler the data handler * @param name the file name * @param mode the access mode (r, rw, rws, rwd) * @return the created object */ public static FileStorage open(DataHandler handler, String name, String mode) { return open(handler, name, mode, null, null, 0); }
private void writeAllRows() { if (file == null) { Database db = session.getDatabase(); String fileName = db.createTempFile(); file = db.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.seek(FileStorage.HEADER_LENGTH); rowBuff = DataBuffer.create(db, Constants.DEFAULT_PAGE_SIZE); file.seek(FileStorage.HEADER_LENGTH); } DataBuffer buff = rowBuff; initBuffer(buff); for (int i = 0, size = list.size(); i < size; i++) { if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) { flushBuffer(buff); initBuffer(buff); } Row r = list.get(i); writeRow(buff, r); } flushBuffer(buff); file.autoDelete(); list.clear(); memory = 0; }
@Override public void close() { if (fileStorage != null) { try { fileStorage.close(); endOfFile = true; } finally { fileStorage = null; } } }
public static void main(String[] args){ FileStorage fileStorage = new FileStorage(); // User create a new file File file1 = new File(); fileStorage.addNewFile(file1, "file1"); // name can also be file1.getName() [...] // Later you can access the file File storedFile = fileStorage.getStoredFile("file1"); }
/** * Close the file (ignoring exceptions) and delete the file. */ public void closeAndDeleteSilently() { if (file != null) { closeSilently(); tempFileDeleter.deleteFile(autoDeleteReference, name); name = null; } }
/** * Open an encrypted file store with the given settings. * * @param handler the data handler * @param name the file name * @param mode the access mode (r, rw, rws, rwd) * @param cipher the name of the cipher algorithm * @param key the encryption key * @param keyIterations the number of iterations the key should be hashed * @return the created object */ public static FileStorage open(DataHandler handler, String name, String mode, String cipher, byte[] key, int keyIterations) { FileStorage store; if (cipher == null) { store = new FileStorage(handler, name, mode); } else { store = new SecureFileStorage(handler, name, mode, cipher, key, keyIterations); } return store; }
ResultDiskBuffer(ServerSession session, SortOrder sort, int columnCount) { this.parent = null; this.sort = sort; this.columnCount = columnCount; Database db = session.getDatabase(); rowBuff = DataBuffer.create(db, Constants.DEFAULT_PAGE_SIZE); String fileName = db.createTempFile(); file = db.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.seek(FileStorage.HEADER_LENGTH); if (sort != null) { tapes = Utils.newSmallArrayList(); mainTape = null; } else { tapes = null; mainTape = new ResultDiskTape(); mainTape.pos = FileStorage.HEADER_LENGTH; } this.maxBufferSize = db.getSettings().largeResultBufferSize; }
private void removeUnusedChunks(TreeSet<Long> removedPages) { int size = removedPages.size(); for (BTreeChunk c : findUnusedChunks(removedPages)) { c.fileStorage.close(); c.fileStorage.delete(); chunks.remove(c.id); chunkIds.clear(c.id); removedPages.removeAll(c.pagePositions); } if (size > removedPages.size()) { writeChunkMetaData(lastChunk.id, removedPages); } }
long start = file.getFilePointer(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int bufferLen = 0; byte[] data = buffer.toByteArray(); buffer.reset(); file.write(data, 0, data.length); bufferLen = 0; file.write(buff.getBytes(), 0, len); file.write(data, 0, data.length); tape.end = file.getFilePointer(); tapes.add(tape); } else { mainTape.end = file.getFilePointer();
/** * Create a CLOB in a temporary file. */ private ValueLob(DataHandler handler, Reader in, long remaining) throws IOException { this.type = Value.CLOB; this.handler = handler; this.small = null; this.lobId = 0; this.hmac = null; this.fileName = createTempLobFileName(handler); this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile.autoDelete(); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, null, null); long tmpPrecision = 0; try { char[] buff = new char[Constants.IO_BUFFER_SIZE]; while (true) { int len = getBufferSize(this.handler, false, remaining); len = IOUtils.readFully(in, buff, len); if (len == 0) { break; } } } finally { out.close(); } this.precision = tmpPrecision; }