/** * 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; } } }
/** * 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 static byte[] toBytes(Map<?, ?> m, Encoding encoding) throws SQLException { ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + 10 * m.size()); byte[] lenBuf = new byte[4]; try { ByteConverter.int4(lenBuf, 0, m.size()); baos.write(lenBuf); for (Entry<?, ?> e : m.entrySet()) { byte[] key = encoding.encode(e.getKey().toString()); ByteConverter.int4(lenBuf, 0, key.length); baos.write(lenBuf); baos.write(key); if (e.getValue() == null) { ByteConverter.int4(lenBuf, 0, -1); baos.write(lenBuf); } else { byte[] val = encoding.encode(e.getValue().toString()); ByteConverter.int4(lenBuf, 0, val.length); baos.write(lenBuf); baos.write(val); } } } catch (IOException ioe) { throw new PSQLException( GT.tr( "Invalid character data was found. This is most likely caused by stored data containing characters that are invalid for the character set the database was created in. The most common example of this is storing 8bit data in a SQL_ASCII database."), PSQLState.DATA_ERROR, ioe); } return baos.toByteArray(); }
@Override public byte[] encodeString(String str) throws SQLException { try { return getEncoding().encode(str); } catch (IOException ioe) { throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."), PSQLState.DATA_ERROR, ioe); } }
public byte[] encodeString(String str) throws SQLException { try { return getEncoding().encode(str); } catch (IOException ioe) { throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."), PSQLState.DATA_ERROR, ioe); } }
public synchronized InputStream getBinaryStream() throws SQLException { checkFreed(); ensureInitialized(); if (_data == null) { return null; } try { return new ByteArrayInputStream(_conn.getEncoding().encode(_data)); } catch (IOException ioe) { // This should be a can't happen exception. We just // decoded this data, so it would be surprising that // we couldn't encode it. // For this reason don't make it translatable. throw new PSQLException("Failed to re-encode xml data.", PSQLState.DATA_ERROR, ioe); } }
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 synchronized InputStream getBinaryStream() throws SQLException { checkFreed(); ensureInitialized(); if (_data == null) return null; try { return new ByteArrayInputStream(_conn.getEncoding().encode(_data)); } catch (IOException ioe) { // This should be a can't happen exception. We just // decoded this data, so it would be surprising that // we couldn't encode it. // For this reason don't make it translatable. throw new PSQLException("Failed to re-encode xml data.", PSQLState.DATA_ERROR, ioe); } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ((len = from.read(cbuf)) >= 0) { if (len > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if (cp.isActive()) { cp.cancelCopy(); } } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
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); } }
/** * 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 byte[] encodeString(String str) throws SQLException { try { return getEncoding().encode(str); } catch (IOException ioe) { throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."), PSQLState.DATA_ERROR, ioe); } }
public byte[] encodeString(String str) throws SQLException { try { return getEncoding().encode(str); } catch (IOException ioe) { throw new PSQLException(GT.tr("Unable to translate data into the desired encoding."), PSQLState.DATA_ERROR, ioe); } }
public synchronized InputStream getBinaryStream() throws SQLException { checkFreed(); ensureInitialized(); if (_data == null) return null; try { return new ByteArrayInputStream(_conn.getEncoding().encode(_data)); } catch (IOException ioe) { // This should be a can't happen exception. We just // decoded this data, so it would be surprising that // we couldn't encode it. // For this reason don't make it translatable. throw new PSQLException("Failed to re-encode xml data.", PSQLState.DATA_ERROR, ioe); } }
public synchronized InputStream getBinaryStream() throws SQLException { checkFreed(); ensureInitialized(); if (_data == null) return null; try { return new ByteArrayInputStream(_conn.getEncoding().encode(_data)); } catch (IOException ioe) { // This should be a can't happen exception. We just // decoded this data, so it would be surprising that // we couldn't encode it. // For this reason don't make it translatable. throw new PSQLException("Failed to re-encode xml data.", PSQLState.DATA_ERROR, ioe); } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
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); } }