/** * Close the underlying reader. */ @Override public void close() { try { reader.close(); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
@Override public FilePathDisk toRealPath() { try { String fileName = new File(name).getCanonicalPath(); return getPath(fileName); } catch (IOException e) { throw DbException.convertIOException(e, name); } }
/** * Call fsync. Depending on the operating system and hardware, this may or * may not in fact write the changes. */ public void sync() { try { file.force(true); } catch (IOException e) { closeFileSilently(); throw DbException.convertIOException(e, name); } }
/** * Close the file. */ public void close() { if (file != null) { try { trace("close", name, file); file.close(); } catch (IOException e) { throw DbException.convertIOException(e, name); } finally { file = null; } } }
/** * Read a statement from the reader. This method returns null if the end has * been reached. * * @return the SQL statement or null */ public String readStatement() { if (endOfFile) { return null; } try { return readStatementLoop(); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
/** * Get the current location of the file pointer. * * @return the location */ public long getFilePointer() { if (SysProperties.CHECK2) { try { if (file.position() != filePos) { DbException.throwInternalError(file.position() + " " + filePos); } } catch (IOException e) { throw DbException.convertIOException(e, name); } } return filePos; }
/** * Create a temporary file in the database folder. * * @return the file name */ public String createTempFile() { try { boolean inTempDir = readOnly; String name = databaseName; if (!persistent) { name = "memFS:" + name; } return FileUtils.createTempFile(name, Constants.SUFFIX_TEMP_FILE, true, inTempDir); } catch (IOException e) { throw DbException.convertIOException(e, databaseName); } }
/** * Go to the specified file location. * * @param pos the location */ public void seek(long pos) { if (SysProperties.CHECK && pos % Constants.FILE_BLOCK_SIZE != 0) { DbException.throwInternalError( "unaligned seek " + name + " pos " + pos); } try { if (pos != filePos) { file.position(pos); filePos = pos; } } catch (IOException e) { throw DbException.convertIOException(e, name); } }
private PrintWriter getWriter(String fileName, String suffix) { fileName = fileName.substring(0, fileName.length() - 3); String outputFile = fileName + suffix; trace("Created file: " + outputFile); try { return new PrintWriter(IOUtils.getBufferedWriter( FileUtils.newOutputStream(outputFile, false))); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
private static void copyFileTo(DataHandler h, String sourceFileName, String targetFileName) { synchronized (h.getLobSyncObject()) { try { IOUtils.copyFiles(sourceFileName, targetFileName); } catch (IOException e) { throw DbException.convertIOException(e, null); } } }
private static ServerSocket createServerSocketTry(int port, boolean ssl) { try { InetAddress bindAddress = getBindAddress(); if (ssl) { return CipherFactory.createServerSocket(port, bindAddress); } if (bindAddress == null) { return new ServerSocket(port); } return new ServerSocket(port, 0, bindAddress); } catch (BindException be) { throw DbException.get(ErrorCode.EXCEPTION_OPENING_PORT_2, be, "" + port, be.toString()); } catch (IOException e) { throw DbException.convertIOException(e, "port: " + port + " ssl: " + ssl); } }
private static void installPgCatalog(Statement stat) throws SQLException { try (Reader r = new InputStreamReader(new ByteArrayInputStream(Utils .getResource("/org/h2/server/pg/pg_catalog.sql")))) { ScriptReader reader = new ScriptReader(r); while (true) { String sql = reader.readStatement(); if (sql == null) { break; } stat.execute(sql); } reader.close(); } catch (IOException e) { throw DbException.convertIOException(e, "Can not read pg_catalog resource"); } }
/** * Read a number of bytes. * * @param b the target buffer * @param off the offset * @param len the number of bytes to read */ public void readFully(byte[] b, int off, int len) { if (SysProperties.CHECK && (len < 0 || len % Constants.FILE_BLOCK_SIZE != 0)) { DbException.throwInternalError( "unaligned read " + name + " len " + len); } checkPowerOff(); try { FileUtils.readFully(file, ByteBuffer.wrap(b, off, len)); } catch (IOException e) { throw DbException.convertIOException(e, name); } filePos += len; }
@Override public byte[] getBytesNoCopy() { if (type == CLOB) { // convert hex to string return super.getBytesNoCopy(); } if (small != null) { return small; } try { return IOUtils.readBytesAndClose( getInputStream(), Integer.MAX_VALUE); } catch (IOException e) { throw DbException.convertIOException(e, fileName); } }
@Override public InputStream getInputStream() { if (small != null) { return new ByteArrayInputStream(small); } else if (fileName != null) { FileStore store = handler.openFile(fileName, "r", true); boolean alwaysClose = SysProperties.lobCloseBetweenReads; return new BufferedInputStream(new FileStoreInputStream(store, handler, false, alwaysClose), Constants.IO_BUFFER_SIZE); } long byteCount = (type == Value.BLOB) ? precision : -1; try { return handler.getLobStorage().getInputStream(this, hmac, byteCount); } catch (IOException e) { throw DbException.convertIOException(e, toString()); } }
@Override public byte[] getBytesNoCopy() { if (type == CLOB) { // convert hex to string return super.getBytesNoCopy(); } if (small != null) { return small; } try { return IOUtils.readBytesAndClose(getInputStream(), Integer.MAX_VALUE); } catch (IOException e) { throw DbException.convertIOException(e, toString()); } }
@Override public String getString() { int len = precision > Integer.MAX_VALUE || precision == 0 ? Integer.MAX_VALUE : (int) precision; try { if (type == Value.CLOB) { if (small != null) { return new String(small, StandardCharsets.UTF_8); } return IOUtils.readStringAndClose(getReader(), len); } byte[] buff; if (small != null) { buff = small; } else { buff = IOUtils.readBytesAndClose(getInputStream(), len); } return StringUtils.convertBytesToHex(buff); } catch (IOException e) { throw DbException.convertIOException(e, fileName); } }
@Override public void reset() { rowId = -1; currentRow = null; if (session == null) { return; } synchronized (session) { session.checkClosed(); try { session.traceOperation("RESULT_RESET", id); transfer.writeInt(SessionRemote.RESULT_RESET).writeInt(id).flush(); } catch (IOException e) { throw DbException.convertIOException(e, null); } } }
public FileStoreInputStream(FileStore store, DataHandler handler, boolean compression, boolean alwaysClose) { this.store = store; this.alwaysClose = alwaysClose; if (compression) { compress = CompressTool.getInstance(); } else { compress = null; } page = Data.create(handler, Constants.FILE_BLOCK_SIZE); try { if (store.length() <= FileStore.HEADER_LENGTH) { close(); } else { fillBuffer(); } } catch (IOException e) { throw DbException.convertIOException(e, store.name); } }
private void remapIfOld() { if (session == null) { return; } try { if (id <= session.getCurrentId() - SysProperties.SERVER_CACHED_OBJECTS / 2) { // object is too old - we need to map it to a new id int newId = session.getNextId(); session.traceOperation("CHANGE_ID", id); transfer.writeInt(SessionRemote.CHANGE_ID).writeInt(id).writeInt(newId); id = newId; // TODO remote result set: very old result sets may be // already removed on the server (theoretically) - how to // solve this? } } catch (IOException e) { throw DbException.convertIOException(e, null); } }