public static String extractComment(BufferedReader reader, CommentHandler... allowedHeaderTypes) throws IOException { reader.mark(READAHEAD_LEN); for (CommentHandler handler : allowedHeaderTypes) { Optional<String> comment = handler.extractHeader(reader); if (!comment.isPresent()) { reader.reset(); } else { return comment.get(); } } return null; } }
@NonNull @Override public Collection<String> toComment(@NonNull Collection<String> lines) { return delegate.toComment(lines); }
@Override public void save(@NonNull ConfigurationNode node) throws IOException { if (sink == null) { throw new IOException("No sink present to write to!"); } try (Writer writer = sink.call()) { if (headerMode != HeaderMode.NONE) { String header = node.getOptions().getHeader(); if (header != null && !header.isEmpty()) { for (String line : getDefaultCommentHandler().toComment(ImmutableList.copyOf(LINE_SPLITTER.split(header)))) { writer.write(line); writer.write(SYSTEM_LINE_SEPARATOR); } writer.write(SYSTEM_LINE_SEPARATOR); } } saveInternal(node, writer); } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } }
@NonNull @Override public Optional<String> extractHeader(@NonNull BufferedReader reader) throws IOException { return delegate.extractHeader(reader); }
@Override public void save(ConfigurationNode node) throws IOException { if (!canSave()) { throw new IOException("No sink present to write to!"); } try (Writer writer = sink.call()) { if (headerMode != HeaderMode.NONE) { String header = node.getOptions().getHeader(); if (header != null && !header.isEmpty()) { for (String line : getDefaultCommentHandler().toComment(ImmutableList.copyOf(LINE_SPLITTER.split(header)))) { writer.write(line); writer.write(SYSTEM_LINE_SEPARATOR); } writer.write(SYSTEM_LINE_SEPARATOR); } } saveInternal(node, writer); } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } }
/** * Uses provided comment handlers to extract a comment from the reader. * * @param reader The reader * @param allowedHeaderTypes The handlers to try * @return The extracted comment, or null if a comment could not be extracted * @throws IOException If an IO error occurs */ @Nullable public static String extractComment(@NonNull BufferedReader reader, @NonNull CommentHandler... allowedHeaderTypes) throws IOException { reader.mark(READAHEAD_LEN); for (CommentHandler handler : allowedHeaderTypes) { Optional<String> comment = handler.extractHeader(reader); if (!comment.isPresent()) { reader.reset(); } else { return comment.get(); } } return null; }
@Override public void save(@NonNull ConfigurationNode node) throws IOException { if (sink == null) { throw new IOException("No sink present to write to!"); } try (Writer writer = sink.call()) { writeHeaderInternal(writer); if (headerMode != HeaderMode.NONE) { String header = node.getOptions().getHeader(); if (header != null && !header.isEmpty()) { for (String line : getDefaultCommentHandler().toComment(ImmutableList.copyOf(LINE_SPLITTER.split(header)))) { writer.write(line); writer.write(SYSTEM_LINE_SEPARATOR); } writer.write(SYSTEM_LINE_SEPARATOR); } } saveInternal(node, writer); } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } }
/** * Uses provided comment handlers to extract a comment from the reader. * * @param reader The reader * @param allowedHeaderTypes The handlers to try * @return The extracted comment, or null if a comment could not be extracted * @throws IOException If an IO error occurs */ @Nullable public static String extractComment(@NonNull BufferedReader reader, @NonNull CommentHandler... allowedHeaderTypes) throws IOException { reader.mark(READAHEAD_LEN); for (CommentHandler handler : allowedHeaderTypes) { Optional<String> comment = handler.extractHeader(reader); if (!comment.isPresent()) { reader.reset(); } else { return comment.get(); } } return null; } }