Refine search
public synchronized InputStream getBinaryStream() throws SQLException { checkFreed(); LargeObject subLO = lo.copy(); subLOs.add(subLO); subLO.seek(0, LargeObject.SEEK_SET); return subLO.getInputStream(); }
protected long createBlob(int i, InputStream inputStream, long length) throws SQLException { LargeObjectManager lom = connection.getLargeObjectAPI(); long oid = lom.createLO(); LargeObject lob = lom.open(oid); OutputStream outputStream = lob.getOutputStream(); byte[] buf = new byte[4096]; try {
public synchronized OutputStream setBinaryStream(long pos) throws SQLException { assertPosition(pos); LargeObject subLO = lo.copy(); subLOs.add(subLO); subLO.seek((int)(pos-1)); return subLO.getOutputStream(); }
LargeObjectManager lom = getLargeObjectManager(conn); if (ob != null) { LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); long size = copy((InputStream) ob, os); lo.truncate((int) size); lo.close(); } else { lom.delete(oid); row.setInt(col, -1); conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); oid = lom.create(); LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); copy((InputStream)ob, os); lo.close(); row.setInt(col, oid);
public void setBytes(int parameterIndex, byte[] x) throws SQLException { checkClosed(); if (null == x) { setNull(parameterIndex, Types.VARBINARY); return ; } if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports the bytea datatype for byte arrays byte[] copy = new byte[x.length]; System.arraycopy(x, 0, copy, 0, x.length); preparedParameters.setBytea( parameterIndex, copy, 0, x.length); } else { //Version 7.1 and earlier support done as LargeObjects LargeObjectManager lom = connection.getLargeObjectAPI(); long oid = lom.createLO(); LargeObject lob = lom.open(oid); lob.write(x); lob.close(); setLong(parameterIndex, oid); } }
public InputStream getBinaryStream(int columnIndex) throws SQLException { checkResultSet( columnIndex ); if (wasNullFlag) return null; if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports BinaryStream for all PG bytea type //As the spec/javadoc for this method indicate this is to be used for //large binary values (i.e. LONGVARBINARY) PG doesn't have a separate //long binary datatype, but with toast the bytea datatype is capable of //handling very large values. Thus the implementation ends up calling //getBytes() since there is no current way to stream the value from the server byte b[] = getBytes(columnIndex); if (b != null) return new ByteArrayInputStream(b); } else { // In 7.1 Handle as BLOBS so return the LargeObject input stream if ( fields[columnIndex - 1].getOID() == Oid.OID) { LargeObjectManager lom = connection.getLargeObjectAPI(); LargeObject lob = lom.open(getLong(columnIndex)); return lob.getInputStream(); } } return null; }
blob = lom.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); s.execute("DELETE FROM " + METADATA_TABLE + " WHERE key = '" + key + "'"); s.execute("INSERT INTO " + METADATA_TABLE + " (key, value) VALUES('" + key + "', 'BLOB: " + blob + "')"); LargeObject obj = lom.open(blob, LargeObjectManager.WRITE); obj.truncate(0); return new LargeObjectOutputStream(con, obj); } catch (SQLException e) {
@Override public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { synchronized (connection) { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); LargeObject largeObject = null; Long oid = getOID(file); try { connection.setAutoCommit(false); largeObject = lobjManager.open(oid, LargeObjectManager.WRITE); largeObject.seek(largeObject.size()); largeObject.write(data); largeObject.close(); connection.commit(); } catch (Exception e) { connection.rollback(); throw e; } return data.length; } }
private int getPostGresLargeObjectSize(JDBCSequentialFile file) throws SQLException { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); int size = 0; Long oid = getOID(file); if (oid != null) { synchronized (connection) { try { connection.setAutoCommit(false); LargeObject largeObject = lobjManager.open(oid, LargeObjectManager.READ); size = largeObject.size(); largeObject.close(); connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } } return size; } }
protected synchronized LargeObject getLo(boolean forWrite) throws SQLException { if (this.currentLo != null) { if (forWrite && !currentLoIsWriteable) { // Reopen the stream in read-write, at the same pos. int currentPos = this.currentLo.tell(); LargeObjectManager lom = conn.getLargeObjectAPI(); LargeObject newLo = lom.open(oid, LargeObjectManager.READWRITE); this.subLOs.add(this.currentLo); this.currentLo = newLo; if (currentPos != 0) { this.currentLo.seek(currentPos); } } return this.currentLo; } LargeObjectManager lom = conn.getLargeObjectAPI(); currentLo = lom.open(oid, forWrite ? LargeObjectManager.READWRITE : LargeObjectManager.READ); currentLoIsWriteable = forWrite; return currentLo; }
public synchronized java.io.InputStream getBinaryStream(long pos, long length) throws SQLException { checkFreed(); LargeObject subLO = getLo(false).copy(); addSubLO(subLO); if (pos > Integer.MAX_VALUE) { subLO.seek64(pos - 1, LargeObject.SEEK_SET); } else { subLO.seek((int) pos - 1, LargeObject.SEEK_SET); } return subLO.getInputStream(length); }
public void write(int b) throws java.io.IOException { checkClosed(); try { if (bpos >= bsize) { lo.write(buf); bpos = 0; } buf[bpos++] = (byte) b; } catch (SQLException se) { throw new IOException(se.toString()); } }
/** * Writes all or part of the given <code>byte</code> array to the * <code>BLOB</code> value that this <code>Blob</code> object represents * and returns the number of bytes written. * Writing starts at position <code>pos</code> in the <code>BLOB</code> * value; <code>len</code> bytes from the given byte array are written. * * @param pos the position in the <code>BLOB</code> object at which * to start writing * @param bytes the array of bytes to be written to this <code>BLOB</code> * object * @param offset the offset into the array <code>bytes</code> at which * to start reading the bytes to be set * @param len the number of bytes to be written to the <code>BLOB</code> * value from the array of bytes <code>bytes</code> * @return the number of bytes written * @exception SQLException if there is an error accessing the * <code>BLOB</code> value * @see #getBytes * @since 1.4 */ public synchronized int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { assertPosition(pos); lo.seek((int) (pos-1)); lo.write(bytes, offset, len); return len; }
public boolean hasNext() throws SQLException { boolean result = false; if (idx < numBytes) { result = true; } else { numBytes = lo.read(buffer, 0, BUFFER_SIZE); idx = 0; result = (numBytes > 0); } return result; }
/** * For Blobs this should be in bytes while for Clobs it should be * in characters. Since we really haven't figured out how to handle * character sets for Clobs the current implementation uses bytes for * both Blobs and Clobs. */ public synchronized void truncate(long len) throws SQLException { checkFreed(); if (!conn.haveMinimumServerVersion("8.3")) throw new PSQLException(GT.tr("Truncation of large objects is only implemented in 8.3 and later servers."), PSQLState.NOT_IMPLEMENTED); if (len < 0) { throw new PSQLException(GT.tr("Cannot truncate LOB to a negative length."), PSQLState.INVALID_PARAMETER_VALUE); } if (len > Integer.MAX_VALUE) { throw new PSQLException(GT.tr("PostgreSQL LOBs can only index to: {0}", new Integer(Integer.MAX_VALUE)), PSQLState.INVALID_PARAMETER_VALUE); } lo.truncate((int)len); }
public LargeObject copy() throws SQLException { return new LargeObject(fp, oid, mode); }
/** * For Blobs this should be in bytes while for Clobs it should be in characters. Since we really * haven't figured out how to handle character sets for Clobs the current implementation uses * bytes for both Blobs and Clobs. * * @param len maximum length * @throws SQLException if operation fails */ public synchronized void truncate(long len) throws SQLException { checkFreed(); if (!conn.haveMinimumServerVersion(ServerVersion.v8_3)) { throw new PSQLException( GT.tr("Truncation of large objects is only implemented in 8.3 and later servers."), PSQLState.NOT_IMPLEMENTED); } if (len < 0) { throw new PSQLException(GT.tr("Cannot truncate LOB to a negative length."), PSQLState.INVALID_PARAMETER_VALUE); } if (len > Integer.MAX_VALUE) { if (support64bit) { getLo(true).truncate64(len); } else { throw new PSQLException(GT.tr("PostgreSQL LOBs can only index to: {0}", Integer.MAX_VALUE), PSQLState.INVALID_PARAMETER_VALUE); } } else { getLo(true).truncate((int) len); } }