mbien commented on code in PR #8304: URL: https://github.com/apache/netbeans/pull/8304#discussion_r1985743240
########## ide/parsing.api/src/org/netbeans/modules/parsing/api/Source.java: ########## @@ -334,81 +332,72 @@ public Snapshot createSnapshot () { // The code below is a copy of CharacterConversions.lineSeparatorToLineFeed() method // and it handles line-end conversion. In general EditorKits can do more conversions, // but they usually don't and this should be good enough for Snapshots. - try { - if (fileObject.isValid ()) { - if (fileObject.getSize() <= Utilities.getMaxFileSize()) { - final InputStream is = fileObject.getInputStream (); - assert is != null : "FileObject.getInputStream() returned null for FileObject: " + FileUtil.getFileDisplayName(fileObject); //NOI18N - try { - InputStreamReader reader = new InputStreamReader ( - is, - FileEncodingQuery.getEncoding (fileObject) - ); - try { - StringBuilder output = new StringBuilder(Math.max(16, (int) fileObject.getSize())); - List<Integer> lso = new LinkedList<Integer>(); - boolean lastCharCR = false; - char [] buffer = new char [65536]; - int size = -1; - - final char LF = '\n'; //NOI18N, Unicode line feed (0x000A) - final char CR = '\r'; //NOI18N, Unicode carriage return (0x000D) - final char LS = 0x2028; // Unicode line separator (0x2028) - final char PS = 0x2029; // Unicode paragraph separator (0x2029) - - lso.add(0); - while(-1 != (size = reader.read(buffer, 0, buffer.length))) { - for(int i = 0; i < size; i++) { - char ch = buffer[i]; - if (lastCharCR && ch == LF) { - // CR-LF pair changed to single LF - continue; - } - if (ch == CR) { - // convert to LF; subsequent LF will be skipped - output.append(LF); - lso.add(output.length()); - lastCharCR = true; - } else if (ch == LS || ch == PS) { // Unicode LS, PS - output.append(LF); - lso.add(output.length()); - lastCharCR = false; - } else { // current char not CR - lastCharCR = false; - output.append(ch); - } - } + if (fileObject.isValid()) { + if (fileObject.getSize() <= Utilities.getMaxFileSize()) { + try (InputStreamReader reader = new InputStreamReader(fileObject.getInputStream(), FileEncodingQuery.getEncoding(fileObject))) { + StringBuilder output = new StringBuilder(Math.max(16, (int) fileObject.getSize())); + List<Integer> lso = new ArrayList<>(); + boolean lastCharCR = false; + char[] buffer = new char [65536]; + int size = -1; + + final char LF = '\n'; //NOI18N, Unicode line feed (0x000A) + final char CR = '\r'; //NOI18N, Unicode carriage return (0x000D) + final char LS = 0x2028; // Unicode line separator (0x2028) + final char PS = 0x2029; // Unicode paragraph separator (0x2029) + + lso.add(0); + while(-1 != (size = reader.read(buffer, 0, buffer.length))) { + for(int i = 0; i < size; i++) { + char ch = buffer[i]; + if (lastCharCR && ch == LF) { + // CR-LF pair changed to single LF + continue; } - - int [] lsoArr = new int [lso.size()]; - int idx = 0; - for(Integer offset : lso) { - lsoArr[idx++] = offset; + if (ch == CR) { + // convert to LF; subsequent LF will be skipped + output.append(LF); + lso.add(output.length()); + lastCharCR = true; + } else if (ch == LS || ch == PS) { // Unicode LS, PS + output.append(LF); + lso.add(output.length()); + lastCharCR = false; + } else { // current char not CR + lastCharCR = false; + output.append(ch); } - - text[0] = output; - lineStartOffsets[0] = lsoArr; - } finally { - reader.close (); } - } finally { - is.close (); } - } else { - LOG.log( - Level.WARNING, - "Source {0} of size: {1} has been ignored due to large size. Files large then {2} bytes are ignored, you can increase the size by parse.max.file.size property.", //NOI18N - new Object[]{ - FileUtil.getFileDisplayName(fileObject), - fileObject.getSize(), - Utilities.getMaxFileSize() - }); + + int[] lsoArr = new int[lso.size()]; + int idx = 0; + for(Integer offset : lso) { + lsoArr[idx++] = offset; + } + + text[0] = output; + lineStartOffsets[0] = lsoArr; + } catch (FileNotFoundException fnfe) { + // working with a stale FileObject, just ignore this (eg see #158119) + } catch (CoderMalfunctionError cme) { + text[0] = ""; //NOI18N + lineStartOffsets[0] = new int[] { 0 }; Review Comment: done, used multicatch - FileNotFound is also implicitly handled via IOException. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For additional commands, e-mail: notifications-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists