Re: svn commit: r902341 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r902341 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java

Bilgin Ibryam-2
[hidden email] wrote:

> Author: adrianc
> Date: Sat Jan 23 05:38:09 2010
> New Revision: 902341
>
> URL: http://svn.apache.org/viewvc?rev=902341&view=rev
> Log:
> Small improvement to ClobToString converter.
>
> Modified:
>     ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>
> Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=902341&r1=902340&r2=902341&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java (original)
> +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java Sat Jan 23 05:38:09 2010
> @@ -108,15 +108,13 @@
>          }
>  
>          public String convert(Clob obj) throws ConversionException {
> -            StringBuilder strBuf = new StringBuilder();
> -            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
> -            int charsRead = 0;
> +            char[] charBuffer = null;
>              Reader clobReader = null;
>              try {
> -                clobReader =  obj.getCharacterStream();
> -                while ((charsRead = clobReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
> -                    strBuf.append(inCharBuffer, 0, charsRead);
> -                }
> +                int clobLength = (int) obj.length();
> +                charBuffer = new char[clobLength];
> +                clobReader = obj.getCharacterStream();
> +                clobReader.read(charBuffer, 0, clobLength);
>              } catch (Exception e) {
>                  throw new ConversionException(e);
>              }
> @@ -127,7 +125,7 @@
>                      } catch (IOException e) {}
>                  }
>              }
> -            return strBuf.toString();
> +            return new String(charBuffer);
>          }
>      }
>  
>
>
>  
Adrian, there is a problem with this improvement:

the following line

clobReader.read(charBuffer, 0, clobLength);

can't read more than 8192 chars when used with derby database (I didn't check with other databases).
The bug appeared when I tried to read big xml file stored in ElectronicText entity and got the xml truncated.
Also I think this is the cause for https://issues.apache.org/jira/browse/OFBIZ-3529

Bilgin
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r902341 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java

Adrian Crum
Bilgin Ibryam wrote:

> [hidden email] wrote:
>> Author: adrianc
>> Date: Sat Jan 23 05:38:09 2010
>> New Revision: 902341
>>
>> URL: http://svn.apache.org/viewvc?rev=902341&view=rev
>> Log:
>> Small improvement to ClobToString converter.
>>
>> Modified:
>>    
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>
>>
>> Modified:
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=902341&r1=902340&r2=902341&view=diff 
>>
>> ==============================================================================
>>
>> ---
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>> (original)
>> +++
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>> Sat Jan 23 05:38:09 2010
>> @@ -108,15 +108,13 @@
>>          }
>>  
>>          public String convert(Clob obj) throws ConversionException {
>> -            StringBuilder strBuf = new StringBuilder();
>> -            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
>> -            int charsRead = 0;
>> +            char[] charBuffer = null;
>>              Reader clobReader = null;
>>              try {
>> -                clobReader =  obj.getCharacterStream();
>> -                while ((charsRead = clobReader.read(inCharBuffer, 0,
>> CHAR_BUFFER_SIZE)) > 0) {
>> -                    strBuf.append(inCharBuffer, 0, charsRead);
>> -                }
>> +                int clobLength = (int) obj.length();
>> +                charBuffer = new char[clobLength];
>> +                clobReader = obj.getCharacterStream();
>> +                clobReader.read(charBuffer, 0, clobLength);
>>              } catch (Exception e) {
>>                  throw new ConversionException(e);
>>              }
>> @@ -127,7 +125,7 @@
>>                      } catch (IOException e) {}
>>                  }
>>              }
>> -            return strBuf.toString();
>> +            return new String(charBuffer);
>>          }
>>      }
>>  
>>
>>
>>  
> Adrian, there is a problem with this improvement:
>
> the following line
>
> clobReader.read(charBuffer, 0, clobLength);
>
> can't read more than 8192 chars when used with derby database (I didn't
> check with other databases).
> The bug appeared when I tried to read big xml file stored in
> ElectronicText entity and got the xml truncated.
> Also I think this is the cause for
> https://issues.apache.org/jira/browse/OFBIZ-3529

I will look into it.

-Adrian

Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r902341 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java

Adam Heath-2
Adrian Crum wrote:

> Bilgin Ibryam wrote:
>> [hidden email] wrote:
>>> Author: adrianc
>>> Date: Sat Jan 23 05:38:09 2010
>>> New Revision: 902341
>>>
>>> URL: http://svn.apache.org/viewvc?rev=902341&view=rev
>>> Log:
>>> Small improvement to ClobToString converter.
>>>
>>> Modified:
>>>    
>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>
>>>
>>> Modified:
>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>
>>> URL:
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=902341&r1=902340&r2=902341&view=diff
>>>
>>> ==============================================================================
>>>
>>> ---
>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>> (original)
>>> +++
>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>> Sat Jan 23 05:38:09 2010
>>> @@ -108,15 +108,13 @@
>>>          }
>>>  
>>>          public String convert(Clob obj) throws ConversionException {
>>> -            StringBuilder strBuf = new StringBuilder();
>>> -            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
>>> -            int charsRead = 0;
>>> +            char[] charBuffer = null;
>>>              Reader clobReader = null;
>>>              try {
>>> -                clobReader =  obj.getCharacterStream();
>>> -                while ((charsRead = clobReader.read(inCharBuffer, 0,
>>> CHAR_BUFFER_SIZE)) > 0) {
>>> -                    strBuf.append(inCharBuffer, 0, charsRead);
>>> -                }
>>> +                int clobLength = (int) obj.length();
>>> +                charBuffer = new char[clobLength];
>>> +                clobReader = obj.getCharacterStream();
>>> +                clobReader.read(charBuffer, 0, clobLength);
>>>              } catch (Exception e) {
>>>                  throw new ConversionException(e);
>>>              }
>>> @@ -127,7 +125,7 @@
>>>                      } catch (IOException e) {}
>>>                  }
>>>              }
>>> -            return strBuf.toString();
>>> +            return new String(charBuffer);
>>>          }
>>>      }
>>>  
>>>
>>>
>>>  
>> Adrian, there is a problem with this improvement:
>>
>> the following line
>>
>> clobReader.read(charBuffer, 0, clobLength);
>>
>> can't read more than 8192 chars when used with derby database (I
>> didn't check with other databases).
>> The bug appeared when I tried to read big xml file stored in
>> ElectronicText entity and got the xml truncated.
>> Also I think this is the cause for
>> https://issues.apache.org/jira/browse/OFBIZ-3529
>
> I will look into it.

You have to always handle a short read, always.  Reintroduce the loop.

Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r902341 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java

Scott Gray-2
On 10/03/2010, at 9:40 AM, Adam Heath wrote:

> Adrian Crum wrote:
>> Bilgin Ibryam wrote:
>>> [hidden email] wrote:
>>>> Author: adrianc
>>>> Date: Sat Jan 23 05:38:09 2010
>>>> New Revision: 902341
>>>>
>>>> URL: http://svn.apache.org/viewvc?rev=902341&view=rev
>>>> Log:
>>>> Small improvement to ClobToString converter.
>>>>
>>>> Modified:
>>>>
>>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>>
>>>>
>>>> Modified:
>>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>>
>>>> URL:
>>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=902341&r1=902340&r2=902341&view=diff
>>>>
>>>> ==============================================================================
>>>>
>>>> ---
>>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>> (original)
>>>> +++
>>>> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
>>>> Sat Jan 23 05:38:09 2010
>>>> @@ -108,15 +108,13 @@
>>>>         }
>>>>
>>>>         public String convert(Clob obj) throws ConversionException {
>>>> -            StringBuilder strBuf = new StringBuilder();
>>>> -            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
>>>> -            int charsRead = 0;
>>>> +            char[] charBuffer = null;
>>>>             Reader clobReader = null;
>>>>             try {
>>>> -                clobReader =  obj.getCharacterStream();
>>>> -                while ((charsRead = clobReader.read(inCharBuffer, 0,
>>>> CHAR_BUFFER_SIZE)) > 0) {
>>>> -                    strBuf.append(inCharBuffer, 0, charsRead);
>>>> -                }
>>>> +                int clobLength = (int) obj.length();
>>>> +                charBuffer = new char[clobLength];
>>>> +                clobReader = obj.getCharacterStream();
>>>> +                clobReader.read(charBuffer, 0, clobLength);
>>>>             } catch (Exception e) {
>>>>                 throw new ConversionException(e);
>>>>             }
>>>> @@ -127,7 +125,7 @@
>>>>                     } catch (IOException e) {}
>>>>                 }
>>>>             }
>>>> -            return strBuf.toString();
>>>> +            return new String(charBuffer);
>>>>         }
>>>>     }
>>>>
>>>>
>>>>
>>>>
>>> Adrian, there is a problem with this improvement:
>>>
>>> the following line
>>>
>>> clobReader.read(charBuffer, 0, clobLength);
>>>
>>> can't read more than 8192 chars when used with derby database (I
>>> didn't check with other databases).
>>> The bug appeared when I tried to read big xml file stored in
>>> ElectronicText entity and got the xml truncated.
>>> Also I think this is the cause for
>>> https://issues.apache.org/jira/browse/OFBIZ-3529
>>
>> I will look into it.
>
> You have to always handle a short read, always.  Reintroduce the loop.
>
Apache Commons IOUtils has some methods for copying from a Reader to a Writer which might be helpful.

Regards
Scott

smime.p7s (3K) Download Attachment