/** * Send a fixed-size array of bytes to the backend. If buf.length < siz, * pad with zeros. If buf.lengh > siz, truncate the array. * * @param buf the array of bytes to be sent * @param siz the number of bytes to be sent * @exception IOException if an I/O error occurs */ public void Send(byte buf[], int siz) throws IOException { Send(buf, 0, siz); }
private void sendCloseStatement(String statementName) throws IOException { // // Send Close. // if (logger.logDebug()) { logger.debug(" FE=> CloseStatement(" + statementName + ")"); } byte[] encodedStatementName = Utils.encodeUTF8(statementName); // Total size = 4 (size field) + 1 (close type, 'S') + N + 1 (statement name) pgStream.SendChar('C'); // Close pgStream.SendInteger4(4 + 1 + encodedStatementName.length + 1); // message size pgStream.SendChar('S'); // Close (Statement) pgStream.Send(encodedStatementName); // statement to close pgStream.SendChar(0); // statement name terminator }
private void sendStartupPacket(PGStream pgStream, String user, String database, Logger logger) throws IOException { // 4: total size including self // 2: protocol major // 2: protocol minor // 64: database name // 32: user name // 64: options // 64: unused // 64: tty if (logger.logDebug()) logger.debug(" FE=> StartupPacket(user=" + user + ",database=" + database + ")"); pgStream.SendInteger4(4 + 4 + 64 + 32 + 64 + 64 + 64); pgStream.SendInteger2(2); // protocol major pgStream.SendInteger2(0); // protocol minor pgStream.Send(database.getBytes("UTF-8"), 64); pgStream.Send(user.getBytes("UTF-8"), 32); pgStream.Send(new byte[64]); // options pgStream.Send(new byte[64]); // unused pgStream.Send(new byte[64]); // tty pgStream.flush(); }
for (int i = 0; i < encodedParams.length; ++i) pgStream.Send(encodedParams[i]); pgStream.SendChar(0);
private static void streamBytea(PGStream pgStream, StreamWrapper wrapper) throws IOException { byte[] rawData = wrapper.getBytes(); if (rawData != null) { pgStream.Send(rawData, wrapper.getOffset(), wrapper.getLength()); return ; } pgStream.SendStream(wrapper.getStream(), wrapper.getLength()); }
void writeV2FastpathValue(int index, PGStream pgStream) throws IOException { --index; if (paramValues[index] instanceof StreamWrapper) { StreamWrapper wrapper = (StreamWrapper)paramValues[index]; pgStream.SendInteger4(wrapper.getLength()); copyStream(pgStream, wrapper); } else if (paramValues[index] instanceof byte[]) { byte[] data = (byte[])paramValues[index]; pgStream.SendInteger4(data.length); pgStream.Send(data); } else if (paramValues[index] instanceof String) { byte[] data = pgStream.getEncoding().encode((String)paramValues[index]); pgStream.SendInteger4(data.length); pgStream.Send(data); } else { throw new IllegalArgumentException("don't know how to stream parameter " + index); } }
private void sendClosePortal(String portalName) throws IOException { // // Send Close. // if (logger.logDebug()) { logger.debug(" FE=> ClosePortal(" + portalName + ")"); } byte[] encodedPortalName = (portalName == null ? null : Utils.encodeUTF8(portalName)); int encodedSize = (encodedPortalName == null ? 0 : encodedPortalName.length); // Total size = 4 (size field) + 1 (close type, 'P') + 1 + N (portal name) pgStream.SendChar('C'); // Close pgStream.SendInteger4(4 + 1 + 1 + encodedSize); // message size pgStream.SendChar('P'); // Close (Portal) if (encodedPortalName != null) pgStream.Send(encodedPortalName); pgStream.SendChar(0); // unnamed portal }
pgStream.SendInteger4((int)encodedSize); // Message size if (encodedPortalName != null) pgStream.Send(encodedPortalName); // Destination portal name. pgStream.SendChar(0); // End of portal name. if (encodedStatementName != null) pgStream.Send(encodedStatementName); // Source statement name. pgStream.SendChar(0); // End of statement name.
private void copyStream(PGStream pgStream, StreamWrapper wrapper) throws IOException { byte[] rawData = wrapper.getBytes(); if (rawData != null) { pgStream.Send(rawData, wrapper.getOffset(), wrapper.getLength()); return ; } pgStream.SendStream(wrapper.getStream(), wrapper.getLength()); }
void writeV3Value(int index, PGStream pgStream) throws IOException { --index; // Null? if (paramValues[index] == NULL_OBJECT) throw new IllegalArgumentException("can't writeV3Value() on a null parameter"); // Directly encoded? if (paramValues[index] instanceof byte[]) { pgStream.Send((byte[])paramValues[index]); return ; } // Binary-format bytea? if (paramValues[index] instanceof StreamWrapper) { streamBytea(pgStream, (StreamWrapper)paramValues[index]); return ; } // Encoded string. if (encoded[index] == null) encoded[index] = Utils.encodeUTF8((String)paramValues[index]); pgStream.Send(encoded[index]); }
private void sendExecute(SimpleQuery query, Portal portal, int limit) throws IOException { // // Send Execute. // if (logger.logDebug()) { logger.debug(" FE=> Execute(portal=" + portal + ",limit=" + limit + ")"); } byte[] encodedPortalName = (portal == null ? null : portal.getEncodedPortalName()); int encodedSize = (encodedPortalName == null ? 0 : encodedPortalName.length); // Total size = 4 (size field) + 1 + N (source portal) + 4 (max rows) pgStream.SendChar('E'); // Execute pgStream.SendInteger4(4 + 1 + encodedSize + 4); // message size if (encodedPortalName != null) pgStream.Send(encodedPortalName); // portal name pgStream.SendChar(0); // portal name terminator pgStream.SendInteger4(limit); // row limit pendingExecuteQueue.add(new Object[] { query, portal }); }
private void sendDescribePortal(SimpleQuery query, Portal portal) throws IOException { // // Send Describe. // if (logger.logDebug()) { logger.debug(" FE=> Describe(portal=" + portal + ")"); } byte[] encodedPortalName = (portal == null ? null : portal.getEncodedPortalName()); // Total size = 4 (size field) + 1 (describe type, 'P') + N + 1 (portal name) int encodedSize = 4 + 1 + (encodedPortalName == null ? 0 : encodedPortalName.length) + 1; pgStream.SendChar('D'); // Describe pgStream.SendInteger4(encodedSize); // message size pgStream.SendChar('P'); // Describe (Portal) if (encodedPortalName != null) pgStream.Send(encodedPortalName); // portal name to close pgStream.SendChar(0); // end of portal name pendingDescribePortalQueue.add(query); query.setPortalDescribed(true); }
private void sendDescribeStatement(SimpleQuery query, SimpleParameterList params, boolean describeOnly) throws IOException { // Send Statement Describe if (logger.logDebug()) { logger.debug(" FE=> Describe(statement=" + query.getStatementName()+")"); } byte[] encodedStatementName = query.getEncodedStatementName(); // Total size = 4 (size field) + 1 (describe type, 'S') + N + 1 (portal name) int encodedSize = 4 + 1 + (encodedStatementName == null ? 0 : encodedStatementName.length) + 1; pgStream.SendChar('D'); // Describe pgStream.SendInteger4(encodedSize); // Message size pgStream.SendChar('S'); // Describe (Statement); if (encodedStatementName != null) pgStream.Send(encodedStatementName); // Statement name pgStream.SendChar(0); // end message pendingDescribeStatementQueue.add(new Object[]{query, params, new Boolean(describeOnly), query.getStatementName()}); pendingDescribePortalQueue.add(query); query.setStatementDescribed(true); query.setPortalDescribed(true); }
/** * Sends given query to BE to start, initialize and lock connection for a CopyOperation. * @param sql COPY FROM STDIN / COPY TO STDOUT statement * @return CopyIn or CopyOut operation object * @throws SQLException on failure */ public synchronized CopyOperation startCopy(String sql, boolean suppressBegin) throws SQLException { waitOnLock(); if (!suppressBegin) { doSubprotocolBegin(); } byte buf[] = Utils.encodeUTF8(sql); try { if (logger.logDebug()) logger.debug(" FE=> Query(CopyStart)"); pgStream.SendChar('Q'); pgStream.SendInteger4(buf.length + 4 + 1); pgStream.Send(buf); pgStream.SendChar(0); pgStream.flush(); return processCopyResults(null, true); // expect a CopyInResponse or CopyOutResponse to our query above } catch(IOException ioe) { throw new PSQLException(GT.tr("Database connection failed when starting copy"), PSQLState.CONNECTION_FAILURE, ioe); } }
/** * Sends data during a live COPY IN operation. Only unlocks the connection if server * suddenly returns CommandComplete, which should not happen * @param op the CopyIn operation presumably currently holding lock on this connection * @param data bytes to send * @param off index of first byte to send (usually 0) * @param siz number of bytes to send (usually data.length) * @throws SQLException on failure */ public synchronized void writeToCopy(CopyInImpl op, byte[] data, int off, int siz) throws SQLException { if(!hasLock(op)) throw new PSQLException(GT.tr("Tried to write to an inactive copy operation"), PSQLState.OBJECT_NOT_IN_STATE); if (logger.logDebug()) logger.debug(" FE=> CopyData(" + siz + ")"); try { pgStream.SendChar('d'); pgStream.SendInteger4(siz + 4); pgStream.Send(data, off, siz); processCopyResults(op, false); // collect any pending notifications without blocking } catch(IOException ioe) { throw new PSQLException(GT.tr("Database connection failed when writing to copy"), PSQLState.CONNECTION_FAILURE, ioe); } }
pgStream.SendChar('f'); // CopyFail pgStream.SendInteger4(5 + msg.length); pgStream.Send(msg); pgStream.SendChar(0); pgStream.flush();
pgStream.Send(outToken); pgStream.flush();