Package org.postgresql.copy

# Class PGCopyOutputStream

java.lang.Object
java.io.OutputStream
org.postgresql.copy.PGCopyOutputStream

All Implemented Interfaces:
  `Closeable`, `Flushable`, `AutoCloseable`, `CopyIn`, `CopyOperation`

public class PGCopyOutputStream extends OutputStream implements CopyIn
OutputStream for buffered input into a PostgreSQL COPY FROM STDIN operation.

## Constructor Summary

Constructors
Constructor
Description
`PGCopyOutputStream(CopyIn op)`
Use given CopyIn operation for writing.
`PGCopyOutputStream(CopyIn op, int bufferSize)`
Use given CopyIn operation for writing.
`PGCopyOutputStream(PGConnection connection, String sql)`
Uses given connection for specified COPY FROM STDIN operation.
`PGCopyOutputStream(PGConnection connection, String sql, int bufferSize)`
Uses given connection for specified COPY FROM STDIN operation.

## Method Summary

Modifier and Type
Method
Description
`void`
`cancelCopy()`
Cancels this copy operation, discarding any exchanged data.
`void`
`close()`
`long`
`endCopy()`
Finishes copy operation successfully.
`void`
`flush()`
`void`
`flushCopy()`
Force any buffered output to be sent over the network to the backend.
`int`
`getFieldCount()`
`int`
`getFieldFormat(int field)`
`int`
`getFormat()`
`long`
`getHandledRowCount()`
After successful end of copy, returns the number of database records handled in that operation.
`boolean`
`isActive()`
`void`
`write(byte[] buf)`
`void`
`write(byte[] buf, int off, int siz)`
`void`
`write(int b)`
`void`
`writeToCopy(byte[] buf, int off, int siz)`
Writes specified part of given byte array to an open and writable copy operation.
`void`
`writeToCopy(ByteStreamWriter from)`
Writes a ByteStreamWriter to an open and writable copy operation.

### Methods inherited from class java.io.OutputStream

`nullOutputStream`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### PGCopyOutputStream

public PGCopyOutputStream(PGConnection connection, String sql) throws SQLException
Uses given connection for specified COPY FROM STDIN operation.

Parameters:
  `connection` - database connection to use for copying (protocol version 3 required)
  `sql` - COPY FROM STDIN statement
Throws:
  `SQLException` - if initializing the operation fails

### PGCopyOutputStream

public PGCopyOutputStream(PGConnection connection, String sql, int bufferSize) throws SQLException
Uses given connection for specified COPY FROM STDIN operation.

Parameters:
  `connection` - database connection to use for copying (protocol version 3 required)
  `sql` - COPY FROM STDIN statement
  `bufferSize` - try to send this many bytes at a time
Throws:
  `SQLException` - if initializing the operation fails

### PGCopyOutputStream

public PGCopyOutputStream(CopyIn op)
Use given CopyIn operation for writing.

Parameters:
  `op` - COPY FROM STDIN operation

### PGCopyOutputStream

public PGCopyOutputStream(CopyIn op, int bufferSize)
Use given CopyIn operation for writing.

Parameters:
  `op` - COPY FROM STDIN operation
  `bufferSize` - try to send this many bytes at a time

## Method Details

### write

public void write(int b) throws IOException

Specified by:
  `write` in class `OutputStream`
Throws:
  `IOException`

### write

public void write(byte[] buf) throws IOException

Overrides:
  `write` in class `OutputStream`
Throws:
  `IOException`

### write

public void write(byte[] buf, int off, int siz) throws IOException

Overrides:
  `write` in class `OutputStream`
Throws:
  `IOException`

### close

public void close() throws IOException

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Overrides:
  `close` in class `OutputStream`
Throws:
  `IOException`

### flush

public void flush() throws IOException

Specified by:
  `flush` in interface `Flushable`
Overrides:
  `flush` in class `OutputStream`
Throws:
  `IOException`

### writeToCopy

public void writeToCopy(byte[] buf, int off, int siz) throws SQLException
Description copied from interface: `CopyIn`
Writes specified part of given byte array to an open and writable copy operation.

Specified by:
  `writeToCopy` in interface `CopyIn`
Parameters:
  `buf` - array of bytes to write
  `off` - offset of first byte to write (normally zero)
  `siz` - number of bytes to write (normally buf.length)
Throws:
  `SQLException` - if the operation fails

### writeToCopy

public void writeToCopy(ByteStreamWriter from) throws SQLException
Description copied from interface: `CopyIn`
Writes a ByteStreamWriter to an open and writable copy operation.

Specified by:
  `writeToCopy` in interface `CopyIn`
Parameters:
  `from` - the source of bytes, e.g. a ByteBufferByteStreamWriter
Throws:
  `SQLException` - if the operation fails

### getFormat

public int getFormat()

Specified by:
  `getFormat` in interface `CopyOperation`
Returns:
  overall format of each row: 0 = textual, 1 = binary

### getFieldFormat

public int getFieldFormat(int field)

Specified by:
  `getFieldFormat` in interface `CopyOperation`
Parameters:
  `field` - number of field (0..fieldCount()-1)
Returns:
  format of requested field: 0 = textual, 1 = binary

### cancelCopy

public void cancelCopy() throws SQLException
Description copied from interface: `CopyOperation`
Cancels this copy operation, discarding any exchanged data.

Specified by:
  `cancelCopy` in interface `CopyOperation`
Throws:
  `SQLException` - if cancelling fails

### getFieldCount

public int getFieldCount()

Specified by:
  `getFieldCount` in interface `CopyOperation`
Returns:
  number of fields in each row for this operation

### isActive

public boolean isActive()

Specified by:
  `isActive` in interface `CopyOperation`
Returns:
  is connection reserved for this Copy operation?

### flushCopy

public void flushCopy() throws SQLException
Description copied from interface: `CopyIn`
Force any buffered output to be sent over the network to the backend. In general this is a useless operation as it will get pushed over in due time or when endCopy is called. Some specific modified server versions (Truviso) want this data sooner. If you are unsure if you need to use this method, don't.

Specified by:
  `flushCopy` in interface `CopyIn`
Throws:
  `SQLException` - if the operation fails.

### endCopy

public long endCopy() throws SQLException
Description copied from interface: `CopyIn`
Finishes copy operation successfully.

Specified by:
  `endCopy` in interface `CopyIn`
Returns:
  number of updated rows for server 8.2 or newer (see getHandledRowCount())
Throws:
  `SQLException` - if the operation fails.

### getHandledRowCount

public long getHandledRowCount()
Description copied from interface: `CopyOperation`
After successful end of copy, returns the number of database records handled in that operation. Only implemented in PostgreSQL server version 8.2 and up. Otherwise, returns -1.

Specified by:
  `getHandledRowCount` in interface `CopyOperation`
Returns:
  number of handled rows or -1