Java Code Examples for java.io.InputStreamReader.skip()

The following are Jave code examples for showing how to use skip() of the java.io.InputStreamReader class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: HTMLJavadocParser.java   View Source Code Vote up 4 votes
private static String getTextFromURLStream(JavadocHelper.TextStream page, int[] offsets, String charset) throws IOException {
    if (page == null)
        return null;

    InputStream fis = null;
    InputStreamReader fisreader = null;
    try {
        fis = page.openStream();
        fisreader = charset == null ? new InputStreamReader(fis) : new InputStreamReader(fis, charset);
        
        StringBuilder sb = new StringBuilder();
        int offset = 0;

        for (int i = 0; i < offsets.length - 1; i+=2) {
            int startOffset = offsets[i];
            int endOffset = offsets[i + 1];
            if (startOffset < 0 || endOffset < 0)
                continue;
            if (startOffset > endOffset) {
                LOG.log(Level.WARNING,
                        "Was not able to correctly parse javadoc: {0}, startOffset={1}, endOffset={2}.",
                        new Object[] {page.getLocation(), startOffset, endOffset});
                return null;
            }

            int len = endOffset - startOffset;
            char buffer[] = new char[len];
            int bytesToSkip = startOffset - offset;
            long bytesSkipped = 0;
            do {
                bytesSkipped = fisreader.skip(bytesToSkip);
                bytesToSkip -= bytesSkipped;
            } while ((bytesToSkip > 0) && (bytesSkipped > 0));

            int bytesAlreadyRead = 0;
            do {
                int count = fisreader.read(buffer, bytesAlreadyRead, len - bytesAlreadyRead);
                if (count < 0){
                    break;
                }
                bytesAlreadyRead += count;
            } while (bytesAlreadyRead < len);
            sb.append(buffer);
            offset = endOffset;
        }
        return sb.toString();
    } finally {
        if (fisreader != null)
            fisreader.close();
    }
}