/** * Send a fixed-size array of bytes to the backend. If {@code buf.length < siz}, pad with zeros. * If {@code buf.lengh > siz}, truncate the array. * * @param buf the array of bytes to be sent * @param siz the number of bytes to be sent * @throws 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. // LOGGER.log(Level.FINEST, " FE=> CloseStatement({0})", 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 }
pgStream.sendInteger2(0); // protocol minor for (byte[] encodedParam : encodedParams) { pgStream.send(encodedParam); 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()); }
private void sendSSPIResponse(byte[] outToken) throws IOException { /* * The sspiContext now contains a token we can send to the server to start the handshake. Send a * 'password' message containing the required data; the server knows we're doing SSPI * negotiation and will deal with it appropriately. */ pgStream.sendChar('p'); pgStream.sendInteger4(4 + outToken.length); pgStream.send(outToken); pgStream.flush(); }
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 sendClosePortal(String portalName) throws IOException { // // Send Close. // LOGGER.log(Level.FINEST, " FE=> ClosePortal({0})", 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 }
private void sendDescribePortal(SimpleQuery query, Portal portal) throws IOException { // // Send Describe. // LOGGER.log(Level.FINEST, " FE=> Describe(portal={0})", 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); }
public void processServerFirstMessage(int length) throws IOException, PSQLException { String serverFirstMessage = pgStream.receiveString(length); LOGGER.log(Level.FINEST, " <=BE AuthenticationSASLContinue( {0} )", serverFirstMessage); try { serverFirstProcessor = scramSession.receiveServerFirstMessage(serverFirstMessage); } catch (ScramException e) { throw new PSQLException( GT.tr("Invalid server-first-message: {0}", serverFirstMessage), PSQLState.CONNECTION_REJECTED, e ); } if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, " <=BE AuthenticationSASLContinue(salt={0}, iterations={1})", new Object[] { serverFirstProcessor.getSalt(), serverFirstProcessor.getIteration() } ); } clientFinalProcessor = serverFirstProcessor.clientFinalProcessor(password); String clientFinalMessage = clientFinalProcessor.clientFinalMessage(); LOGGER.log(Level.FINEST, " FE=> SASLResponse( {0} )", clientFinalMessage); byte[] clientFinalMessageBytes = clientFinalMessage.getBytes(StandardCharsets.UTF_8); sendAuthenticationMessage( clientFinalMessageBytes.length, s -> s.send(clientFinalMessageBytes) ); }
private void sendExecute(SimpleQuery query, Portal portal, int limit) throws IOException { // // Send Execute. // if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, " FE=> Execute(portal={0},limit={1})", new Object[]{portal, 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 ExecuteRequest(query, portal, false)); }
private void sendSimpleQuery(SimpleQuery query, SimpleParameterList params) throws IOException { String nativeSql = query.toString(params); LOGGER.log(Level.FINEST, " FE=> SimpleQuery(query=\"{0}\")", nativeSql); Encoding encoding = pgStream.getEncoding(); byte[] encoded = encoding.encode(nativeSql); pgStream.sendChar('Q'); pgStream.sendInteger4(encoded.length + 4 + 1); pgStream.send(encoded); pgStream.sendChar(0); pgStream.flush(); pendingExecuteQueue.add(new ExecuteRequest(query, null, true)); pendingDescribePortalQueue.add(query); }
public void sendScramClientFirstMessage() throws IOException { String clientFirstMessage = scramSession.clientFirstMessage(); LOGGER.log(Level.FINEST, " FE=> SASLInitialResponse( {0} )", clientFirstMessage); String scramMechanismName = scramClient.getScramMechanism().getName(); byte[] scramMechanismNameBytes = scramMechanismName.getBytes(StandardCharsets.UTF_8); byte[] clientFirstMessageBytes = clientFirstMessage.getBytes(StandardCharsets.UTF_8); sendAuthenticationMessage( (scramMechanismNameBytes.length + 1) + 4 + clientFirstMessageBytes.length, s -> { s.send(scramMechanismNameBytes); s.sendChar(0); // List terminated in '\0' s.sendInteger4(clientFirstMessageBytes.length); s.send(clientFirstMessageBytes); } ); }
private void sendDescribeStatement(SimpleQuery query, SimpleParameterList params, boolean describeOnly) throws IOException { // Send Statement Describe LOGGER.log(Level.FINEST, " FE=> Describe(statement={0})", 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 // Note: statement name can change over time for the same query object // Thus we take a snapshot of the query name pendingDescribeStatementQueue.add( new DescribeRequest(query, params, 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 { LOGGER.log(Level.FINEST, " 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(CopyOperationImpl 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); } LOGGER.log(Level.FINEST, " FE=> CopyData({0})", 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();