commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Tripodi <simonetrip...@apache.org>
Subject Re: A proposal for Bug Commons IO/IO-354
Date Tue, 09 Apr 2013 15:44:32 GMT
Hi Peter,

Thanks for contributing to apache commons!

code snippets don't properly work in email messages, the best way to
contribute code is by submitting patches, attaching them in JIRA issue -
test cases are recommended! :)

HTH,
-Simo


http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/


On Tue, Apr 9, 2013 at 2:37 PM, Peter Liu <liusz0503@gmail.com> wrote:

> Hi, I'm new to Commons-IO, I wanna try a simple patch for the
> issue<
> https://issues.apache.org/jira/browse/IO-354?page=com.atlassian.jirafisheyeplugin:fisheye-issuepanel
> >(IO-354)
> which had been analyzed carefully by reporter.
>
> as reporter described, due to contacting the chars directly, current
> implementation doesn't support multi-byte encoded String. I want to adopt
> ByteArrayOutputStream , a fake stream, to solve this . that's the snippet:
> ------------------------------------
> private long readLines(final RandomAccessFile reader) throws IOException {
>         long pos = reader.getFilePointer();
>         long rePos = pos; // position to re-read
>         ByteArrayOutputStream lineBuf = null;
>         int num;
>         boolean seenCR = false;
>         try{
>          lineBuf = new ByteArrayOutputStream(64);
>          while (getRun() && ((num = reader.read(inbuf)) != -1)) {
>                  for (int i = 0; i < num; i++) {
>                      final byte ch = inbuf[i];
>                      switch (ch) {
>                      case '\n':
>                          seenCR = false; // swallow CR before LF
>                          listener.handle(new
> String(lineBuf.toByteArray()));
>                          lineBuf.reset();
>                          rePos = pos + i + 1;
>                          break;
>                      case '\r':
>                          if (seenCR) {
>                           lineBuf.write('\r');
>                          }
>                          seenCR = true;
>                          break;
>                      default:
>                          if (seenCR) {
>                              seenCR = false; // swallow final CR
>                              listener.handle(new
> String(lineBuf.toByteArray()));
>                              lineBuf.reset();
>                              rePos = pos + i + 1;
>                          }
>                          lineBuf.write(ch);
>                      }
>                  }
>                  pos = reader.getFilePointer();
>              }
>         }finally{
>          /*
>          * close the stream for coding conventions and further
> implementations though
>          * ByteArrayOutputSteram.close() is an empty method and take no
> effects.
>          * Actually there is no more other exceptions would be threw since
> adopting
>          * ByteArrayOutputStream, So this "finally" block is not in pair
> with "catch" block.
>          */
>          IOUtils.closeQuietly(lineBuf);
>         }
>         reader.seek(rePos); // Ensure we can re-read if necessary
>         return rePos;
>     }
> ------------------------------------
>
> I think, The ByteArrayOutputStream is actually an "extendable char array"
> here: no more resources would be occupied except an array inside the class.
>
> At first I considered using a Collection to hold the bytes rather than
> ByteArrayOutputStream. The Constructor of String can only support the
> primitive char array ,However,  the Collection can only be declared holding
> Char, the Wrapper. Seem that a manual loop for converting that to primitive
> is inevitable since commons-collection is not in the build path. To keep
> succinct and avoid the redundant code that has been already implemented by
> other basic component, I chose ByteArrayOutputStream.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message