Thread: BlobInputStream.java patch

BlobInputStream.java patch

From
Chad David
Date:
The current implementation of BlobInputStream does
not properly handle 8-bit unsigned data as it blindly
casts the byte to an int, which java most helpfully
promotes to a signed type.  This causes problems when
you can only return -1 to indicated EOF.

The following patch fixes the bug and has been tested
locally on image data.

Chad David
Guild Software Inc.
davidc@guild.ab.ca


*** BlobInputStream.java    Thu Feb 22 11:11:23 2001
--- BlobInputStream.patched Thu Feb 22 11:10:38 2001
***************
*** 58,73 ****
     */
    public int read() throws java.io.IOException {
      try {
!       if(buffer==null || bpos>=buffer.length) {
          buffer=lo.read(bsize);
          bpos=0;
        }

        // Handle EOF
!       if(bpos>=buffer.length)
          return -1;

!       return (int) buffer[bpos++];
      } catch(SQLException se) {
        throw new IOException(se.toString());
      }
--- 58,81 ----
     */
    public int read() throws java.io.IOException {
      try {
!       if (buffer == null || bpos >= buffer.length) {
          buffer=lo.read(bsize);
          bpos=0;
        }

        // Handle EOF
!       if(bpos >= buffer.length) {
          return -1;
+       }

!       int ret = (buffer[bpos] & 0x7F);
!       if ((buffer[bpos] &0x80) == 0x80) {
!         ret |= 0x80;
!       }
!
!       bpos++;
!
!       return ret;
      } catch(SQLException se) {
        throw new IOException(se.toString());
      }
***************
*** 153,156 ****
    return true;
      }

! }
--- 161,164 ----
    return true;
      }

! }



Re: BlobInputStream.java patch

From
Bruce Momjian
Date:
Peter, can you comment on this?


> The current implementation of BlobInputStream does
> not properly handle 8-bit unsigned data as it blindly
> casts the byte to an int, which java most helpfully
> promotes to a signed type.  This causes problems when
> you can only return -1 to indicated EOF.
>
> The following patch fixes the bug and has been tested
> locally on image data.
>
> Chad David
> Guild Software Inc.
> davidc@guild.ab.ca
>
>
> *** BlobInputStream.java    Thu Feb 22 11:11:23 2001
> --- BlobInputStream.patched Thu Feb 22 11:10:38 2001
> ***************
> *** 58,73 ****
>      */
>     public int read() throws java.io.IOException {
>       try {
> !       if(buffer==null || bpos>=buffer.length) {
>           buffer=lo.read(bsize);
>           bpos=0;
>         }
>
>         // Handle EOF
> !       if(bpos>=buffer.length)
>           return -1;
>
> !       return (int) buffer[bpos++];
>       } catch(SQLException se) {
>         throw new IOException(se.toString());
>       }
> --- 58,81 ----
>      */
>     public int read() throws java.io.IOException {
>       try {
> !       if (buffer == null || bpos >= buffer.length) {
>           buffer=lo.read(bsize);
>           bpos=0;
>         }
>
>         // Handle EOF
> !       if(bpos >= buffer.length) {
>           return -1;
> +       }
>
> !       int ret = (buffer[bpos] & 0x7F);
> !       if ((buffer[bpos] &0x80) == 0x80) {
> !         ret |= 0x80;
> !       }
> !
> !       bpos++;
> !
> !       return ret;
>       } catch(SQLException se) {
>         throw new IOException(se.toString());
>       }
> ***************
> *** 153,156 ****
>     return true;
>       }
>
> ! }
> --- 161,164 ----
>     return true;
>       }
>
> ! }
>
>
>


--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: BlobInputStream.java patch

From
Bruce Momjian
Date:
Thanks.  Patch applied for 7.2.

> The current implementation of BlobInputStream does
> not properly handle 8-bit unsigned data as it blindly
> casts the byte to an int, which java most helpfully
> promotes to a signed type.  This causes problems when
> you can only return -1 to indicated EOF.
>
> The following patch fixes the bug and has been tested
> locally on image data.
>
> Chad David
> Guild Software Inc.
> davidc@guild.ab.ca
>
>
> *** BlobInputStream.java    Thu Feb 22 11:11:23 2001
> --- BlobInputStream.patched Thu Feb 22 11:10:38 2001
> ***************
> *** 58,73 ****
>      */
>     public int read() throws java.io.IOException {
>       try {
> !       if(buffer==null || bpos>=buffer.length) {
>           buffer=lo.read(bsize);
>           bpos=0;
>         }
>
>         // Handle EOF
> !       if(bpos>=buffer.length)
>           return -1;
>
> !       return (int) buffer[bpos++];
>       } catch(SQLException se) {
>         throw new IOException(se.toString());
>       }
> --- 58,81 ----
>      */
>     public int read() throws java.io.IOException {
>       try {
> !       if (buffer == null || bpos >= buffer.length) {
>           buffer=lo.read(bsize);
>           bpos=0;
>         }
>
>         // Handle EOF
> !       if(bpos >= buffer.length) {
>           return -1;
> +       }
>
> !       int ret = (buffer[bpos] & 0x7F);
> !       if ((buffer[bpos] &0x80) == 0x80) {
> !         ret |= 0x80;
> !       }
> !
> !       bpos++;
> !
> !       return ret;
>       } catch(SQLException se) {
>         throw new IOException(se.toString());
>       }
> ***************
> *** 153,156 ****
>     return true;
>       }
>
> ! }
> --- 161,164 ----
>     return true;
>       }
>
> ! }
>
>
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026