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()); } }
public void write(byte[] buf, int off, int len) throws java.io.IOException { checkClosed(); try { // If we have any internally buffered data, send it first if (bpos > 0) { flush(); } if (off == 0 && len == buf.length) { lo.write(buf); // save a buffer creation and copy since full buffer written } else { lo.write(buf, off, len); } } catch (SQLException se) { throw new IOException(se.toString()); } }
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()); } }
public void write(byte[] buf, int off, int len) throws java.io.IOException { checkClosed(); try { // If we have any internally buffered data, send it first if ( bpos > 0 ) flush(); if ( off == 0 && len == buf.length ) lo.write(buf); // save a buffer creation and copy since full buffer written else lo.write(buf, off, len); } catch (SQLException se) { throw new IOException(se.toString()); } }
/** * Flushes this output stream and forces any buffered output bytes to be written out. The general * contract of <code>flush</code> is that calling it is an indication that, if any bytes * previously written have been buffered by the implementation of the output stream, such bytes * should immediately be written to their intended destination. * * @throws IOException if an I/O error occurs. */ public void flush() throws IOException { checkClosed(); try { if (bpos > 0) { lo.write(buf, 0, bpos); } bpos = 0; } catch (SQLException se) { throw new IOException(se.toString()); } }
/** * Flushes this output stream and forces any buffered output bytes * to be written out. The general contract of <code>flush</code> is * that calling it is an indication that, if any bytes previously * written have been buffered by the implementation of the output * stream, such bytes should immediately be written to their * intended destination. * * @exception IOException if an I/O error occurs. */ public void flush() throws IOException { checkClosed(); try { if (bpos > 0) lo.write(buf, 0, bpos); bpos = 0; } 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 synchronized int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { assertPosition(pos); getLo(true).seek((int) (pos - 1)); getLo(true).write(bytes, offset, len); return len; } }
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); } }
@Override public void write(byte[] b, int off, int len) throws IOException { try { obj.write(b, off, len); } catch (SQLException e) { IOException e2 = new IOException("Error writing to large object"); e2.initCause(e); throw e2; } }
@Override public void write(byte[] b, int off, int len) throws IOException { try { obj.write(b, off, len); } catch (SQLException e) { IOException e2 = new IOException("Error writing to large object"); e2.initCause(e); throw e2; } }
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()); } }
public void write(byte[] buf, int off, int len) throws java.io.IOException { checkClosed(); try { // If we have any internally buffered data, send it first if ( bpos > 0 ) flush(); if ( off == 0 && len == buf.length ) lo.write(buf); // save a buffer creation and copy since full buffer written else lo.write(buf, off, len); } catch (SQLException se) { throw new IOException(se.toString()); } }
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()); } }
public void write(byte[] buf, int off, int len) throws java.io.IOException { checkClosed(); try { // If we have any internally buffered data, send it first if ( bpos > 0 ) flush(); if ( off == 0 && len == buf.length ) lo.write(buf); // save a buffer creation and copy since full buffer written else lo.write(buf, off, len); } catch (SQLException se) { throw new IOException(se.toString()); } }
/** * Flushes this output stream and forces any buffered output bytes * to be written out. The general contract of <code>flush</code> is * that calling it is an indication that, if any bytes previously * written have been buffered by the implementation of the output * stream, such bytes should immediately be written to their * intended destination. * * @exception IOException if an I/O error occurs. */ public void flush() throws IOException { checkClosed(); try { if (bpos > 0) lo.write(buf, 0, bpos); bpos = 0; } catch (SQLException se) { throw new IOException(se.toString()); } }
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException { Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test01", "postgres", "postgres"); conn.setAutoCommit(false); File file = new File("/home/user/Pictures/somePicture.jpg"); FileInputStream fis = new FileInputStream(file); LargeObjectManager lom = PGConnection.class.cast(conn).getLargeObjectAPI(); long oid = lom.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); LargeObject lob = lom.open(oid, LargeObjectManager.WRITE); byte[] buffer = new byte[2048]; int s = 0; while ((s = fis.read(buffer, 0, buffer.length)) > 0) { lob.write(buffer, 0, s); } lob.close(); fis.close(); PreparedStatement ps = conn.prepareStatement("insert into test(id, name, content) values (nextval('test_id_seq'), ?, ?)"); ps.setString(1, "foto01"); ps.setLong(2, oid); ps.executeUpdate(); ps.close(); conn.commit(); }
LargeObjectManager largeObjectManager = null; LargeObject largeObject = null; Long largeObjectRef = null; largeObjectManager = ((org.postgresql.PGConnection) connection).getLargeObjectAPI(); largeObjectRef = largeObjectManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); largeObject = largeObjectManager.open(largeObjectRef, LargeObjectManager.WRITE); largeObject.write("string/file content to write into oid column".getBytes()); largeObject.close();
@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; } }
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); } }