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.

Reply via email to