You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2010/01/23 06:38:09 UTC

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

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);
         }
     }
 



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

Posted by Scott Gray <sc...@hotwaxmedia.com>.
On 10/03/2010, at 9:40 AM, Adam Heath wrote:

> Adrian Crum wrote:
>> Bilgin Ibryam wrote:
>>> adrianc@apache.org 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

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

Posted by Adam Heath <do...@brainfood.com>.
Adrian Crum wrote:
> Bilgin Ibryam wrote:
>> adrianc@apache.org 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.


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

Posted by Adrian Crum <ad...@hlmksw.com>.
Bilgin Ibryam wrote:
> adrianc@apache.org 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


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

Posted by Bilgin Ibryam <bi...@gmail.com>.
adrianc@apache.org 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