public static DecodeResult decode(byte[] bytes, int offset, int length) { Encoding defaultEncoding = Encoding.defaultEncoding(); for (Translation tr : FATAL_TRANSLATIONS) { for (String encoding : tr.encodings) { Encoding encoder = Encoding.getDatabaseEncoding(encoding); if (encoder == defaultEncoding) { continue; byte[] encoded; try { byte[] tmp = encoder.encode(tr.fatalText); encoded = new byte[tmp.length + 2]; encoded[0] = 'S'; for (String text : tr.texts) { try { byte[] textBytes = encoder.encode(text); if (arrayContains(bytes, offset, length, textBytes, 0, textBytes.length)) { foundOne = true; String decoded = encoder.decode(bytes, offset, length); if (decoded.indexOf(65533) != -1) { return new DecodeResult(decoded, encoder.name()); } catch (IOException e) {
if (isAvailable(candidates[i])) return new Encoding(candidates[i]); if (isAvailable(databaseEncoding)) return new Encoding(databaseEncoding); return defaultEncoding();
/** * Change the encoding used by this connection. * * @param encoding the new encoding to use * @throws IOException if something goes wrong */ public void setEncoding(Encoding encoding) throws IOException { if (this.encoding != null && this.encoding.name().equals(encoding.name())) { return; } // Close down any old writer. if (encodingWriter != null) { encodingWriter.close(); } this.encoding = encoding; // Intercept flush() downcalls from the writer; our caller // will call PGStream.flush() as needed. OutputStream interceptor = new FilterOutputStream(pg_output) { public void flush() throws IOException { } public void close() throws IOException { super.flush(); } }; encodingWriter = encoding.getEncodingWriter(interceptor); }
private void runInitialQueries(ProtocolConnectionImpl protoConnection, Properties info, Logger logger) throws SQLException, IOException { byte[][] results = SetupQueryRunner.run(protoConnection, "set datestyle = 'ISO'; select version(), case when pg_encoding_to_char(1) = 'SQL_ASCII' then 'UNKNOWN' else getdatabaseencoding() end", true); String rawDbVersion = protoConnection.getEncoding().decode(results[0]); StringTokenizer versionParts = new StringTokenizer(rawDbVersion); protoConnection.setEncoding(Encoding.getDatabaseEncoding("UTF8")); String dbEncoding = (results[1] == null ? null : protoConnection.getEncoding().decode(results[1])); if (logger.logDebug()) protoConnection.setEncoding(Encoding.getJVMEncoding(charSet)); protoConnection.setEncoding(Encoding.getDatabaseEncoding(dbEncoding)); protoConnection.setEncoding(Encoding.defaultEncoding()); String value = protoConnection.getEncoding().decode(results[0]); protoConnection.setStandardConformingStrings(value.equalsIgnoreCase("on"));
/** * Decode an array of bytes into a string. * * @param encodedString a bytearray containing the encoded string the string to encod * @return the decoded string * @throws IOException if something goes wrong */ public String decode(byte[] encodedString) throws IOException { return decode(encodedString, 0, encodedString.length); }
/** * Checks weather this encoding is compatible with ASCII for the number characters '-' and * '0'..'9'. Where compatible means that they are encoded with exactly same values. * * @return If faster ASCII number parsing can be used with this encoding. */ private boolean testAsciiNumbers() { // TODO: test all postgres supported encoding to see if there are // any which do _not_ have ascii numbers in same location // at least all the encoding listed in the encodings hashmap have // working ascii numbers try { String test = "-0123456789"; byte[] bytes = encode(test); String res = new String(bytes, "US-ASCII"); return test.equals(res); } catch (java.io.UnsupportedEncodingException e) { return false; } catch (IOException e) { return false; } } }
/** * Receives a fixed-size string from the backend, and tries to avoid "UTF-8 decode failed" * errors. * * @param len the length of the string to receive, in bytes. * @return the decoded string * @throws IOException if something wrong happens */ public EncodingPredictor.DecodeResult receiveErrorString(int len) throws IOException { if (!pg_input.ensureBytes(len)) { throw new EOFException(); } EncodingPredictor.DecodeResult res; try { String value = encoding.decode(pg_input.getBuffer(), pg_input.getIndex(), len); // no autodetect warning as the message was converted on its own res = new EncodingPredictor.DecodeResult(value, null); } catch (IOException e) { res = EncodingPredictor.decode(pg_input.getBuffer(), pg_input.getIndex(), len); if (res == null) { Encoding enc = Encoding.defaultEncoding(); String value = enc.decode(pg_input.getBuffer(), pg_input.getIndex(), len); res = new EncodingPredictor.DecodeResult(value, enc.name()); } } pg_input.skip(len); return res; }
public int getInt(int columnIndex) throws SQLException { checkResultSet(columnIndex); if (wasNullFlag) return 0; // SQL NULL Encoding encoding = connection.getEncoding(); if (encoding.hasAsciiNumbers()) { try { return getFastInt(columnIndex); } catch (NumberFormatException ex) { } } return toInt( getFixedString(columnIndex) ); }
public synchronized Reader getCharacterStream() throws SQLException { Charset connectionCharset = Charset.forName(conn.getEncoding().name()); return new InputStreamReader(getBinaryStream(), connectionCharset); }
protected Encoding(String encoding) { this.encoding = encoding; fastASCIINumbers = testAsciiNumbers(); }
/** * Constructor: Connect to the PostgreSQL back end and return * a stream connection. * * @param host the hostname to connect to * @param port the port number that the postmaster is sitting on * @exception IOException if an IOException occurs below it. */ public PGStream(String host, int port) throws IOException { this.host = host; this.port = port; changeSocket(new Socket(host, port)); setEncoding(Encoding.getJVMEncoding("US-ASCII")); _int2buf = new byte[2]; _int4buf = new byte[4]; }
value); pgStream.setEncoding(Encoding.getDatabaseEncoding(value)); } else if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
/** * Change the encoding used by this connection. * * @param encoding the new encoding to use * @throws IOException if something goes wrong */ public void setEncoding(Encoding encoding) throws IOException { // Close down any old writer. if (encodingWriter != null) encodingWriter.close(); this.encoding = encoding; // Intercept flush() downcalls from the writer; our caller // will call PGStream.flush() as needed. OutputStream interceptor = new FilterOutputStream(pg_output) { public void flush() throws IOException { } public void close() throws IOException { super.flush(); } }; encodingWriter = encoding.getEncodingWriter(interceptor); }
private void runInitialQueries(ProtocolConnectionImpl protoConnection, String charSet, Logger logger) throws SQLException, IOException { byte[][] results = runSetupQuery(protoConnection, "set datestyle = 'ISO'; select version(), case when pg_encoding_to_char(1) = 'SQL_ASCII' then 'UNKNOWN' else getdatabaseencoding() end", true); String rawDbVersion = protoConnection.getEncoding().decode(results[0]); StringTokenizer versionParts = new StringTokenizer(rawDbVersion); protoConnection.setEncoding(Encoding.getDatabaseEncoding("UNICODE")); String dbEncoding = (results[1] == null ? null : protoConnection.getEncoding().decode(results[1])); if (logger.logDebug()) protoConnection.setEncoding(Encoding.getJVMEncoding(charSet)); protoConnection.setEncoding(Encoding.getDatabaseEncoding(dbEncoding)); protoConnection.setEncoding(Encoding.defaultEncoding()); String value = protoConnection.getEncoding().decode(results[0]); protoConnection.setStandardConformingStrings(value.equalsIgnoreCase("on"));
/** * Decode an array of bytes into a string. * * @param encodedString a byte array containing the string to decode * @return the decoded string * @throws IOException if something goes wrong */ public String decode(byte[] encodedString) throws IOException { return decode(encodedString, 0, encodedString.length); }
/** * Checks weather this encoding is compatible with ASCII for the number * characters '-' and '0'..'9'. Where compatible means that they are encoded * with exactly same values. * * @return If faster ASCII number parsing can be used with this encoding. */ private boolean testAsciiNumbers() { // TODO: test all postgres supported encoding to see if there are // any which do _not_ have ascii numbers in same location // at least all the encoding listed in the encodings hashmap have // working ascii numbers try { String test = "-0123456789"; byte[] bytes = encode(test); String res = new String(bytes, "US-ASCII"); return test.equals(res); } catch (java.io.UnsupportedEncodingException e) { return false; } catch (IOException e) { return false; } } }
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { checkResultSet(columnIndex); if (wasNullFlag) return null; Encoding encoding = connection.getEncoding(); if (encoding.hasAsciiNumbers()) { try { return getFastBigDecimal(columnIndex); } catch (NumberFormatException ex) { } } return toBigDecimal( getFixedString(columnIndex), scale ); }