Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 38604 invoked from network); 15 Jun 2007 12:36:16 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Jun 2007 12:36:16 -0000 Received: (qmail 25010 invoked by uid 500); 15 Jun 2007 12:36:20 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 24938 invoked by uid 500); 15 Jun 2007 12:36:20 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 24925 invoked by uid 99); 15 Jun 2007 12:36:20 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 Jun 2007 05:36:20 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 Jun 2007 05:36:15 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 9523E1A981A; Fri, 15 Jun 2007 05:35:55 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r547653 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java Date: Fri, 15 Jun 2007 12:35:55 -0000 To: derby-commits@db.apache.org From: kristwaa@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070615123555.9523E1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kristwaa Date: Fri Jun 15 05:35:54 2007 New Revision: 547653 URL: http://svn.apache.org/viewvc?view=rev&rev=547653 Log: DERBY-2824: Improve error reporting, fix whitespace/formatting issues and replace tabs in UTF8Reader. Replaces the tabs with spaces. Patch file: derby-2824-1a-tabs_to_spaces.diff Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java?view=diff&rev=547653&r1=547652&r2=547653 ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java Fri Jun 15 05:35:54 2007 @@ -38,36 +38,36 @@ public final class UTF8Reader extends Reader { - private InputStream in; + private InputStream in; /** Stream store that can reposition itself on request. */ private final PositionedStoreStream positionedIn; /** Store last visited position in the store stream. */ private long rawStreamPos = 0L; - private final long utfLen; // bytes - private long utfCount; // bytes - private long readerCharCount; // characters - private long maxFieldSize; // characeters - - private char[] buffer = new char[8 * 1024]; - private int charactersInBuffer; // within buffer - private int readPositionInBuffer; + private final long utfLen; // bytes + private long utfCount; // bytes + private long readerCharCount; // characters + private long maxFieldSize; // characeters + + private char[] buffer = new char[8 * 1024]; + private int charactersInBuffer; // within buffer + private int readPositionInBuffer; - private boolean noMoreReads; + private boolean noMoreReads; // maintain a reference to the parent object so that it can't get // garbage collected until we are done with the stream. private ConnectionChild parent; - public UTF8Reader( + public UTF8Reader( InputStream in, - long maxFieldSize, + long maxFieldSize, ConnectionChild parent, - Object synchronization) + Object synchronization) throws IOException, SQLException - { - super(synchronization); - this.maxFieldSize = maxFieldSize; - this.parent = parent; + { + super(synchronization); + this.maxFieldSize = maxFieldSize; + this.parent = parent; parent.setupContextStack(); try { @@ -103,7 +103,7 @@ } finally { parent.restoreContextStack(); } - } + } /** * Constructs a UTF8Reader using a stream. @@ -140,193 +140,193 @@ this.in = new BufferedInputStream(in); } - /* - ** Reader implemention. - */ - public int read() throws IOException - { - synchronized (lock) { - - // check if closed.. - if (noMoreReads) - throw new IOException(); - - if (readPositionInBuffer >= charactersInBuffer) { - if (fillBuffer()) { - return -1; - } - readPositionInBuffer = 0; - } - - return buffer[readPositionInBuffer++]; - } - } - - public int read(char[] cbuf, int off, int len) throws IOException - { - synchronized (lock) { - // check if closed.. - if (noMoreReads) - throw new IOException(); - - if (readPositionInBuffer >= charactersInBuffer) { - if (fillBuffer()) { - return -1; - } - readPositionInBuffer = 0; - } - - int remainingInBuffer = charactersInBuffer - readPositionInBuffer; - - if (len > remainingInBuffer) - len = remainingInBuffer; - - System.arraycopy(buffer, readPositionInBuffer, cbuf, off, len); - readPositionInBuffer += len; - - return len; - } - } - - public long skip(long len) throws IOException { - if (len < 0) { - throw new IllegalArgumentException( - "Number of characters to skip must be positive:" + len); - } - synchronized (lock) { - // check if closed.. - if (noMoreReads) - throw new IOException(); - - if (readPositionInBuffer >= charactersInBuffer) { - // do somthing - if (fillBuffer()) { - return 0L; - } - readPositionInBuffer = 0; - } - - int remainingInBuffer = charactersInBuffer - readPositionInBuffer; - - if (len > remainingInBuffer) - len = remainingInBuffer; - - readPositionInBuffer += len; - - return len; - } - - } - - public void close() - { - synchronized (lock) { - closeIn(); - parent = null; - noMoreReads = true; - } - } - - /* - ** Methods just for Derby's JDBC driver - */ - - public int readInto(StringBuffer sb, int len) throws IOException { - - synchronized (lock) { - if (readPositionInBuffer >= charactersInBuffer) { - if (fillBuffer()) { - return -1; - } - readPositionInBuffer = 0; - } - - int remainingInBuffer = charactersInBuffer - readPositionInBuffer; - - if (len > remainingInBuffer) - len = remainingInBuffer; - sb.append(buffer, readPositionInBuffer, len); - - readPositionInBuffer += len; - - return len; - } - } - int readAsciiInto(byte[] abuf, int off, int len) throws IOException { - - synchronized (lock) { - if (readPositionInBuffer >= charactersInBuffer) { - if (fillBuffer()) { - return -1; - } - readPositionInBuffer = 0; - } - - int remainingInBuffer = charactersInBuffer - readPositionInBuffer; - - if (len > remainingInBuffer) - len = remainingInBuffer; - - char[] lbuffer = buffer; - for (int i = 0; i < len; i++) { - char c = lbuffer[readPositionInBuffer + i]; - byte cb; - if (c <= 255) - cb = (byte) c; - else - cb = (byte) '?'; // Question mark - out of range character. - - abuf[off + i] = cb; - } - - readPositionInBuffer += len; - - return len; - } - } - - /* - ** internal implementation - */ - - - private void closeIn() { - if (in != null) { - try { - in.close(); - } catch (IOException ioe) { - } finally { - in = null; - } - } - } - private IOException utfFormatException(String s) { - noMoreReads = true; - closeIn(); - return new UTFDataFormatException(s); - } - - private IOException utfFormatException() { - noMoreReads = true; - closeIn(); - return new UTFDataFormatException(); - } - - /** - Fill the buffer, return true if eof has been reached. - */ + /* + ** Reader implemention. + */ + public int read() throws IOException + { + synchronized (lock) { + + // check if closed.. + if (noMoreReads) + throw new IOException(); + + if (readPositionInBuffer >= charactersInBuffer) { + if (fillBuffer()) { + return -1; + } + readPositionInBuffer = 0; + } + + return buffer[readPositionInBuffer++]; + } + } + + public int read(char[] cbuf, int off, int len) throws IOException + { + synchronized (lock) { + // check if closed.. + if (noMoreReads) + throw new IOException(); + + if (readPositionInBuffer >= charactersInBuffer) { + if (fillBuffer()) { + return -1; + } + readPositionInBuffer = 0; + } + + int remainingInBuffer = charactersInBuffer - readPositionInBuffer; + + if (len > remainingInBuffer) + len = remainingInBuffer; + + System.arraycopy(buffer, readPositionInBuffer, cbuf, off, len); + readPositionInBuffer += len; + + return len; + } + } + + public long skip(long len) throws IOException { + if (len < 0) { + throw new IllegalArgumentException( + "Number of characters to skip must be positive:" + len); + } + synchronized (lock) { + // check if closed.. + if (noMoreReads) + throw new IOException(); + + if (readPositionInBuffer >= charactersInBuffer) { + // do somthing + if (fillBuffer()) { + return 0L; + } + readPositionInBuffer = 0; + } + + int remainingInBuffer = charactersInBuffer - readPositionInBuffer; + + if (len > remainingInBuffer) + len = remainingInBuffer; + + readPositionInBuffer += len; + + return len; + } + + } + + public void close() + { + synchronized (lock) { + closeIn(); + parent = null; + noMoreReads = true; + } + } + + /* + ** Methods just for Derby's JDBC driver + */ + + public int readInto(StringBuffer sb, int len) throws IOException { + + synchronized (lock) { + if (readPositionInBuffer >= charactersInBuffer) { + if (fillBuffer()) { + return -1; + } + readPositionInBuffer = 0; + } + + int remainingInBuffer = charactersInBuffer - readPositionInBuffer; + + if (len > remainingInBuffer) + len = remainingInBuffer; + sb.append(buffer, readPositionInBuffer, len); + + readPositionInBuffer += len; + + return len; + } + } + int readAsciiInto(byte[] abuf, int off, int len) throws IOException { + + synchronized (lock) { + if (readPositionInBuffer >= charactersInBuffer) { + if (fillBuffer()) { + return -1; + } + readPositionInBuffer = 0; + } + + int remainingInBuffer = charactersInBuffer - readPositionInBuffer; + + if (len > remainingInBuffer) + len = remainingInBuffer; + + char[] lbuffer = buffer; + for (int i = 0; i < len; i++) { + char c = lbuffer[readPositionInBuffer + i]; + byte cb; + if (c <= 255) + cb = (byte) c; + else + cb = (byte) '?'; // Question mark - out of range character. + + abuf[off + i] = cb; + } + + readPositionInBuffer += len; + + return len; + } + } + + /* + ** internal implementation + */ + + + private void closeIn() { + if (in != null) { + try { + in.close(); + } catch (IOException ioe) { + } finally { + in = null; + } + } + } + private IOException utfFormatException(String s) { + noMoreReads = true; + closeIn(); + return new UTFDataFormatException(s); + } + + private IOException utfFormatException() { + noMoreReads = true; + closeIn(); + return new UTFDataFormatException(); + } + + /** + Fill the buffer, return true if eof has been reached. + */ //@GuardedBy("lock") - private boolean fillBuffer() throws IOException - { - if (in == null) - return true; - - charactersInBuffer = 0; - - try { - try { - - parent.setupContextStack(); + private boolean fillBuffer() throws IOException + { + if (in == null) + return true; + + charactersInBuffer = 0; + + try { + try { + + parent.setupContextStack(); // If we are operating on a positioned stream, reposition it to // continue reading at the position we stopped last time. if (this.positionedIn != null) { @@ -337,83 +337,83 @@ } } readChars: - while ( - (charactersInBuffer < buffer.length) && - ((utfCount < utfLen) || (utfLen == 0)) && - ((maxFieldSize == 0) || (readerCharCount < maxFieldSize)) - ) - { - int c = in.read(); - if (c == -1) { - if (utfLen == 0) { - closeIn(); - break readChars; - } - throw utfFormatException(); - } - - int finalChar; - switch (c >> 4) { - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: - // 0xxxxxxx - utfCount++; - finalChar = c; - break; - - case 12: case 13: - { - // 110x xxxx 10xx xxxx - utfCount += 2; - int char2 = in.read(); - if (char2 == -1) - throw utfFormatException(); - - if ((char2 & 0xC0) != 0x80) - throw utfFormatException(); - finalChar = (((c & 0x1F) << 6) | (char2 & 0x3F)); - break; - } - - case 14: - { - // 1110 xxxx 10xx xxxx 10xx xxxx - utfCount += 3; - int char2 = in.read(); - int char3 = in.read(); - if (char2 == -1 || char3 == -1) - throw utfFormatException(); - - if ((c == 0xE0) && (char2 == 0) && (char3 == 0)) - { - if (utfLen == 0) { - // we reached the end of a long string, - // that was terminated with - // (11100000, 00000000, 00000000) - closeIn(); - break readChars; - } - throw utfFormatException(); - } - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw utfFormatException(); - - finalChar = (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - ((char3 & 0x3F) << 0)); - } - break; - - default: - // 10xx xxxx, 1111 xxxx - throw utfFormatException(); - } - - buffer[charactersInBuffer++] = (char) finalChar; - readerCharCount++; - } - if (utfLen != 0 && utfCount > utfLen) - throw utfFormatException("utfCount " + utfCount + " utfLen " + utfLen); + while ( + (charactersInBuffer < buffer.length) && + ((utfCount < utfLen) || (utfLen == 0)) && + ((maxFieldSize == 0) || (readerCharCount < maxFieldSize)) + ) + { + int c = in.read(); + if (c == -1) { + if (utfLen == 0) { + closeIn(); + break readChars; + } + throw utfFormatException(); + } + + int finalChar; + switch (c >> 4) { + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: + // 0xxxxxxx + utfCount++; + finalChar = c; + break; + + case 12: case 13: + { + // 110x xxxx 10xx xxxx + utfCount += 2; + int char2 = in.read(); + if (char2 == -1) + throw utfFormatException(); + + if ((char2 & 0xC0) != 0x80) + throw utfFormatException(); + finalChar = (((c & 0x1F) << 6) | (char2 & 0x3F)); + break; + } + + case 14: + { + // 1110 xxxx 10xx xxxx 10xx xxxx + utfCount += 3; + int char2 = in.read(); + int char3 = in.read(); + if (char2 == -1 || char3 == -1) + throw utfFormatException(); + + if ((c == 0xE0) && (char2 == 0) && (char3 == 0)) + { + if (utfLen == 0) { + // we reached the end of a long string, + // that was terminated with + // (11100000, 00000000, 00000000) + closeIn(); + break readChars; + } + throw utfFormatException(); + } + + if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) + throw utfFormatException(); + + finalChar = (((c & 0x0F) << 12) | + ((char2 & 0x3F) << 6) | + ((char3 & 0x3F) << 0)); + } + break; + + default: + // 10xx xxxx, 1111 xxxx + throw utfFormatException(); + } + + buffer[charactersInBuffer++] = (char) finalChar; + readerCharCount++; + } + if (utfLen != 0 && utfCount > utfLen) + throw utfFormatException("utfCount " + utfCount + " utfLen " + utfLen); if (charactersInBuffer != 0) { if (this.positionedIn != null) { @@ -421,30 +421,30 @@ // we let go the next time we fill the buffer. this.rawStreamPos = this.positionedIn.getPosition(); } - return false; + return false; } - closeIn(); - return true; - } finally { - parent.restoreContextStack(); - } - } catch (SQLException sqle) { + closeIn(); + return true; + } finally { + parent.restoreContextStack(); + } + } catch (SQLException sqle) { IOException ioe = new IOException(sqle.getSQLState() + ": " + sqle.getMessage()); ioe.initCause(sqle); throw ioe; - } - } + } + } - // this method came from java.io.DataInputStream + // this method came from java.io.DataInputStream private final int readUnsignedShort() throws IOException { - int ch1 = in.read(); - int ch2 = in.read(); - if ((ch1 | ch2) < 0) - throw new EOFException(); + int ch1 = in.read(); + int ch2 = in.read(); + if ((ch1 | ch2) < 0) + throw new EOFException(); - return (ch1 << 8) + (ch2 << 0); - } + return (ch1 << 8) + (ch2 << 0); + } }