Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StringBuffer with StringBuilder #243

Merged
merged 1 commit into from Jan 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions org/postgresql/Driver.java.in
Expand Up @@ -550,8 +550,8 @@ public class Driver implements java.sql.Driver
urlProps.setProperty("PGDBNAME", l_urlServer.substring(slash + 1));

String[] addresses = l_urlServer.substring(0, slash).split(",");
StringBuffer hosts = new StringBuffer();
StringBuffer ports = new StringBuffer();
StringBuilder hosts = new StringBuilder();
StringBuilder ports = new StringBuilder();
for (int addr = 0; addr < addresses.length; ++addr) {
String address = addresses[addr];

Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/core/Parser.java
Expand Up @@ -195,7 +195,7 @@ public static String unmarkDoubleQuestion(String query, boolean standardConformi
if (query == null) return query;

char[] aChars = query.toCharArray();
StringBuffer buf = new StringBuffer(aChars.length);
StringBuilder buf = new StringBuilder(aChars.length);
for(int i=0, j=-1; i< aChars.length; i++)
{
switch (aChars[i])
Expand Down
180 changes: 134 additions & 46 deletions org/postgresql/core/Utils.java
Expand Up @@ -10,11 +10,10 @@
package org.postgresql.core;

import java.sql.SQLException;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import java.text.NumberFormat;
import java.text.ParsePosition;

Expand All @@ -34,7 +33,7 @@ public class Utils {
* @return a hex-encoded printable representation of <code>data</code>
*/
public static String toHexString(byte[] data) {
StringBuffer sb = new StringBuffer(data.length * 2);
StringBuilder sb = new StringBuilder(data.length * 2);
for (int i = 0; i < data.length; ++i)
{
sb.append(Integer.toHexString((data[i] >> 4) & 15));
Expand Down Expand Up @@ -81,50 +80,95 @@ public static byte[] encodeUTF8(String str) {
* @param standardConformingStrings
* @return the sbuf argument; or a new string buffer for sbuf == null
* @throws SQLException if the string contains a <tt>\0</tt> character
* @deprecated use {@link #escapeLiteral(StringBuilder, String, boolean)} instead
*/
public static StringBuffer appendEscapedLiteral(StringBuffer sbuf, String value,
boolean standardConformingStrings)
throws SQLException {
public static StringBuffer appendEscapedLiteral(StringBuffer sbuf, String value, boolean standardConformingStrings)
throws SQLException
{
if (sbuf == null)
{
sbuf = new StringBuffer(value.length() * 11 / 10); // Add 10% for escaping.

if (standardConformingStrings)
}
doAppendEscapedLiteral(sbuf, value, standardConformingStrings);
return sbuf;
}

/**
* Escape the given literal <tt>value</tt> and append it to the string builder
* <tt>sbuf</tt>. If <tt>sbuf</tt> is <tt>null</tt>, a new StringBuilder will be
* returned. The argument <tt>standardConformingStrings</tt> defines whether the
* backend expects standard-conforming string literals or allows backslash
* escape sequences.
*
* @param sbuf the string builder to append to; or <tt>null</tt>
* @param value the string value
* @param standardConformingStrings
* @return the sbuf argument; or a new string builder for sbuf == null
* @throws SQLException if the string contains a <tt>\0</tt> character
*/
public static StringBuilder escapeLiteral(StringBuilder sbuf, String value, boolean standardConformingStrings)
throws SQLException
{
if (sbuf == null)
{
// With standard_conforming_strings on, escape only single-quotes.
for (int i = 0; i < value.length(); ++i)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '\'')
sbuf.append('\'');
sbuf.append(ch);
}
sbuf = new StringBuilder(value.length() * 11 / 10); // Add 10% for escaping.
}
else
doAppendEscapedLiteral(sbuf, value, standardConformingStrings);
return sbuf;
}

/**
* Common part for {@link #appendEscapedLiteral(StringBuffer, String, boolean)} and {@link #escapeLiteral(StringBuilder, String, boolean)}
* @param sbuf Either StringBuffer or StringBuilder as we do not expect any IOException to be thrown
* @param value
* @param standardConformingStrings
* @throws SQLException
*/
private static void doAppendEscapedLiteral(Appendable sbuf, String value, boolean standardConformingStrings)
throws SQLException
{
try
{
// With standard_conforming_string off, escape backslashes and
// single-quotes, but still escape single-quotes by doubling, to
// avoid a security hazard if the reported value of
// standard_conforming_strings is incorrect, or an error if
// backslash_quote is off.
for (int i = 0; i < value.length(); ++i)
if (standardConformingStrings)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '\\' || ch == '\'')
// With standard_conforming_strings on, escape only single-quotes.
for (int i = 0; i < value.length(); ++i)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '\'')
sbuf.append('\'');
sbuf.append(ch);
sbuf.append(ch);
}
}
else
{
// With standard_conforming_string off, escape backslashes and
// single-quotes, but still escape single-quotes by doubling, to
// avoid a security hazard if the reported value of
// standard_conforming_strings is incorrect, or an error if
// backslash_quote is off.
for (int i = 0; i < value.length(); ++i)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '\\' || ch == '\'')
sbuf.append(ch);
sbuf.append(ch);
}
}
}

return sbuf;
catch (IOException e)
{
throw new PSQLException(GT.tr("No IOException expected from StringBuffer or StringBuilder"), PSQLState.UNEXPECTED_ERROR, e);
}
}

/**
* Escape the given identifier <tt>value</tt> and append it to the string
* buffer * <tt>sbuf</tt>. If <tt>sbuf</tt> is <tt>null</tt>, a new
* Escape the given identifier <tt>value</tt> and append it to the string buffer
* <tt>sbuf</tt>. If <tt>sbuf</tt> is <tt>null</tt>, a new
* StringBuffer will be returned. This method is different from
* appendEscapedLiteral in that it includes the quoting required for the
* identifier while appendEscapedLiteral does not.
Expand All @@ -133,27 +177,71 @@ public static StringBuffer appendEscapedLiteral(StringBuffer sbuf, String value,
* @param value the string value
* @return the sbuf argument; or a new string buffer for sbuf == null
* @throws SQLException if the string contains a <tt>\0</tt> character
* @deprecated use {@link #escapeIdentifier(StringBuilder, String)} instead
*/
public static StringBuffer appendEscapedIdentifier(StringBuffer sbuf, String value)
throws SQLException {
throws SQLException
{
if (sbuf == null)
{
sbuf = new StringBuffer(2 + value.length() * 11 / 10); // Add 10% for escaping.
}
doAppendEscapedIdentifier(sbuf, value);
return sbuf;
}

sbuf.append('"');
/**
* Escape the given identifier <tt>value</tt> and append it to the string builder
* <tt>sbuf</tt>. If <tt>sbuf</tt> is <tt>null</tt>, a new
* StringBuilder will be returned. This method is different from
* appendEscapedLiteral in that it includes the quoting required for the
* identifier while {@link #escapeLiteral(StringBuilder, String, boolean)} does not.
*
* @param sbuf the string builder to append to; or <tt>null</tt>
* @param value the string value
* @return the sbuf argument; or a new string builder for sbuf == null
* @throws SQLException if the string contains a <tt>\0</tt> character
*/
public static StringBuilder escapeIdentifier(StringBuilder sbuf, String value)
throws SQLException
{
if (sbuf == null)
{
sbuf = new StringBuilder(2 + value.length() * 11 / 10); // Add 10% for escaping.
}
doAppendEscapedIdentifier(sbuf, value);
return sbuf;
}

for (int i = 0; i < value.length(); ++i)
/**
* Common part for appendEscapedIdentifier
* @param sbuf Either StringBuffer or StringBuilder as we do not expect any IOException to be thrown.
* @param value
* @throws SQLException
*/
private static void doAppendEscapedIdentifier(Appendable sbuf, String value)
throws SQLException
{
try
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in identifiers."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '"')
sbuf.append('"');

for (int i = 0; i < value.length(); ++i)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in identifiers."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '"')
sbuf.append(ch);
sbuf.append(ch);
sbuf.append(ch);
}

sbuf.append('"');
}
catch (IOException e)
{
throw new PSQLException(GT.tr("No IOException expected from StringBuffer or StringBuilder"), PSQLState.UNEXPECTED_ERROR, e);
}

sbuf.append('"');

return sbuf;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions org/postgresql/core/v2/ConnectionFactoryImpl.java
Expand Up @@ -485,17 +485,17 @@ else if (dbEncoding != null)
String appName = PGProperty.APPLICATION_NAME.get(info);
if (appName != null && dbVersion.compareTo("9.0") >= 0)
{
StringBuffer sb = new StringBuffer("SET application_name = '");
Utils.appendEscapedLiteral(sb, appName, protoConnection.getStandardConformingStrings());
StringBuilder sb = new StringBuilder("SET application_name = '");
Utils.escapeLiteral(sb, appName, protoConnection.getStandardConformingStrings());
sb.append("'");
SetupQueryRunner.run(protoConnection, sb.toString(), false);
}

String currentSchema = PGProperty.CURRENT_SCHEMA.get(info);
if (currentSchema != null)
{
StringBuffer sb = new StringBuffer("SET search_path = '");
Utils.appendEscapedLiteral(sb, appName, protoConnection.getStandardConformingStrings());
StringBuilder sb = new StringBuilder("SET search_path = '");
Utils.escapeLiteral(sb, appName, protoConnection.getStandardConformingStrings());
sb.append("'");
SetupQueryRunner.run(protoConnection, sb.toString(), false);
}
Expand Down
4 changes: 2 additions & 2 deletions org/postgresql/core/v2/SimpleParameterList.java
Expand Up @@ -60,12 +60,12 @@ public void setLiteralParameter(int index, String value, int oid) throws SQLExce
}

public void setStringParameter(int index, String value, int oid) throws SQLException {
StringBuffer sbuf = new StringBuffer(2 + value.length() * 11 / 10); // Add 10% for escaping.
StringBuilder sbuf = new StringBuilder(2 + value.length() * 11 / 10); // Add 10% for escaping.

if (useEStringSyntax)
sbuf.append(' ').append('E');
sbuf.append('\'');
Utils.appendEscapedLiteral(sbuf, value, false);
Utils.escapeLiteral(sbuf, value, false);
sbuf.append('\'');

setLiteralParameter(index, sbuf.toString(), oid);
Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/core/v2/V2Query.java
Expand Up @@ -88,7 +88,7 @@ public ParameterList createParameterList() {
}

public String toString(ParameterList parameters) {
StringBuffer sbuf = new StringBuffer(fragments[0]);
StringBuilder sbuf = new StringBuilder(fragments[0]);
for (int i = 1; i < fragments.length; ++i)
{
if (parameters == null)
Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/core/v3/CompositeQuery.java
Expand Up @@ -32,7 +32,7 @@ public ParameterList createParameterList() {
}

public String toString(ParameterList parameters) {
StringBuffer sbuf = new StringBuffer(subqueries[0].toString());
StringBuilder sbuf = new StringBuilder(subqueries[0].toString());
for (int i = 1; i < subqueries.length; ++i)
{
sbuf.append(';');
Expand Down
6 changes: 3 additions & 3 deletions org/postgresql/core/v3/ConnectionFactoryImpl.java
Expand Up @@ -339,7 +339,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf
private void sendStartupPacket(PGStream pgStream, String[][] params, Logger logger) throws IOException {
if (logger.logDebug())
{
StringBuffer details = new StringBuffer();
StringBuilder details = new StringBuilder();
for (int i = 0; i < params.length; ++i)
{
if (i != 0)
Expand Down Expand Up @@ -758,9 +758,9 @@ private void runInitialQueries(ProtocolConnection protoConnection, Properties in

String appName = PGProperty.APPLICATION_NAME.get(info);
if (appName != null && dbVersion >= 90000) {
StringBuffer sql = new StringBuffer();
StringBuilder sql = new StringBuilder();
sql.append("SET application_name = '");
Utils.appendEscapedLiteral(sql, appName, protoConnection.getStandardConformingStrings());
Utils.escapeLiteral(sql, appName, protoConnection.getStandardConformingStrings());
sql.append("'");
SetupQueryRunner.run(protoConnection, sql.toString(), false);
}
Expand Down
4 changes: 2 additions & 2 deletions org/postgresql/core/v3/QueryExecutorImpl.java
Expand Up @@ -1276,7 +1276,7 @@ private void sendParse(SimpleQuery query, SimpleParameterList params, boolean on

if (logger.logDebug())
{
StringBuffer sbuf = new StringBuffer(" FE=> Parse(stmt=" + statementName + ",query=\"");
StringBuilder sbuf = new StringBuilder(" FE=> Parse(stmt=" + statementName + ",query=\"");
for (int i = 0; i < fragments.length; ++i)
{
if (i > 0)
Expand Down Expand Up @@ -1357,7 +1357,7 @@ private void sendBind(SimpleQuery query, SimpleParameterList params,

if (logger.logDebug())
{
StringBuffer sbuf = new StringBuffer(" FE=> Bind(stmt=" + statementName + ",portal=" + portal);
StringBuilder sbuf = new StringBuilder(" FE=> Bind(stmt=" + statementName + ",portal=" + portal);
for (int i = 1; i <= params.getParameterCount(); ++i)
{
sbuf.append(",$").append(i).append("=<").append(params.toString(i)).append(">");
Expand Down
4 changes: 2 additions & 2 deletions org/postgresql/core/v3/SimpleParameterList.java
Expand Up @@ -187,7 +187,7 @@ else if ( (flags[index]& BINARY) == BINARY )
boolean hasBackslash = param.indexOf('\\') != -1;

// add room for quotes + potential escaping.
StringBuffer p = new StringBuffer(3 + param.length() * 11 / 10);
StringBuilder p = new StringBuilder(3 + param.length() * 11 / 10);

boolean standardConformingStrings = false;
boolean supportsEStringSyntax = false;
Expand All @@ -203,7 +203,7 @@ else if ( (flags[index]& BINARY) == BINARY )
p.append('\'');
try
{
p = Utils.appendEscapedLiteral(p, param, standardConformingStrings);
p = Utils.escapeLiteral(p, param, standardConformingStrings);
}
catch (SQLException sqle)
{
Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/core/v3/SimpleQuery.java
Expand Up @@ -36,7 +36,7 @@ public ParameterList createParameterList() {
}

public String toString(ParameterList parameters) {
StringBuffer sbuf = new StringBuffer(fragments[0]);
StringBuilder sbuf = new StringBuilder(fragments[0]);
for (int i = 1; i < fragments.length; ++i)
{
if (parameters == null)
Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/geometric/PGpath.java
Expand Up @@ -139,7 +139,7 @@ public Object clone() throws CloneNotSupportedException
*/
public String getValue()
{
StringBuffer b = new StringBuffer(open ? "[" : "(");
StringBuilder b = new StringBuilder(open ? "[" : "(");

for (int p = 0;p < points.length;p++)
{
Expand Down
2 changes: 1 addition & 1 deletion org/postgresql/geometric/PGpolygon.java
Expand Up @@ -114,7 +114,7 @@ public Object clone() throws CloneNotSupportedException
*/
public String getValue()
{
StringBuffer b = new StringBuffer();
StringBuilder b = new StringBuilder();
b.append("(");
for (int p = 0;p < points.length;p++)
{
Expand Down