public void load(InputStream is, String encoding) throws IOException { Reader in = null; try { in = new InputStreamReader(is, encoding); doLoad(in); } finally { if (in != null) { in.close(); in = null; } } }
/** * Remove the value if present in the first section. * If it were the last value for the key, the key is removed. * If it were the last key, the section is removed. * * @param key the key for the section * @param value the value for the key * @return whether the value was present and removed */ public boolean removeValue(String key, String value) { String section = getSectionName(); return section == null || remove(section, key, value); }
String line = advance(bin); if (line == null) { break; if (isSectionLine(line)) { int splitPos = getSplitPos(line); if (splitPos < 0) { LOGGER.warn("Expected to see '=' in [{}]: {}", sectionName, line); LOGGER.warn("Empty key in [{}]: {}", sectionName, line); String value = more(bin, line.substring(splitPos + 1).trim()); add(sectionName, key, value);
/** * Get the unmodifiable set of keys of the first section. * The set has insertion order. * * @return the keys of the first section */ public Collection<String> getKeys() { IniSection section = getSection(); return section == null ? null : section.getKeys(); }
/** * Get continuation lines, if any. */ private String more(BufferedReader bin, String value) throws IOException { boolean moreCowBell = false; String line = value; StringBuilder buf = new StringBuilder(); do { moreCowBell = more(line); if (moreCowBell) { line = line.substring(0, line.length() - 1).trim(); } buf.append(line); if (moreCowBell) { buf.append('\n'); line = advance(bin); } } while (moreCowBell && line != null); return buf.toString(); }
/** * Get the first value for the key specified by the index and the section. * * @param sectionName the name of the section * @param key the key for the section * @return the value at the specified index * @throws ArrayIndexOutOfBoundsException when the index is out of bounds */ public String getValue(String sectionName, String key) { IniSection section = doGetSection(sectionName); return section == null ? null : section.get(key, 0); }
/** * Replace a value for a key. * A null for key or value is not allowed. * An empty string for a key is not allowed. * * @param sectionName the name of the section * @param key the key for the section * @param value the value for the key * @return {@code true} if the element was added or already was present */ public boolean replace(String sectionName, String key, String value) { IniSection section = getOrCreateSection(sectionName); return section.replace(key, value); }
/** * Get the next line from the input * * @param bin The reader to get data from * @return the next line or null if there is nothing more * @throws IOException if encountered */ private String advance(BufferedReader bin) throws IOException { // Get the next non-blank, non-comment line String trimmed = null; for (String line = bin.readLine(); line != null; line = bin.readLine()) { // Remove leading and trailing whitespace trimmed = line.trim(); // skip blank and comment lines if (!isCommentLine(trimmed)) { return trimmed; } } return null; }
/** * Get the values of a key of the first section. * The collection has insertion order. * Note many keys only have one value. * A key that has no values returns null. * * @param key the key * @return the keyed values or null if the key doesn't exist */ public Collection<String> getValues(String key) { IniSection section = getSection(); return section == null ? null : section.getValues(key); }
/** * Get the value for the key specified by the index and the section. * * @param sectionName the name of the section * @param key the key for the section * @param index the index in the list of values * @return the value at the specified index * @throws ArrayIndexOutOfBoundsException when the index is out of bounds */ public String getValue(String sectionName, String key, int index) { IniSection section = doGetSection(sectionName); return section == null ? null : section.get(key, index); }
/** * Add a key/value pair to a section. * If the section does not exist, it is created. * A null for key or value is not allowed. * An empty string for a key is not allowed. * * @param sectionName the name of the section * @param key the key for the section * @param value the value for the key * @return {@code true} if the element was added or already was present */ public boolean add(String sectionName, String key, String value) { IniSection section = getOrCreateSection(sectionName); return section.add(key, value); }
/** * Remove the key if present. * If it were the last key for the section, the section is removed. * * @param key the key for the section * @return whether the key was present and removed */ public boolean removeValue(String key) { String section = getSectionName(); return section == null || remove(section, key); }
/** * Get the value for the key specified by the index for the first section. * * @param key the key * @param index the index * @return the value at the specified index * @throws ArrayIndexOutOfBoundsException when the index is out of bounds */ public String getValue(String key, int index) { IniSection section = getSection(); return section == null ? null : section.get(key, index); }
public int getValueSize(String sectionName, String key) { IniSection section = doGetSection(sectionName); return section == null ? 0 : section.size(key); }
/** * Get the number of values for a key in the first section * * @param key the key * @return the number of values for a key in the first section */ public int getValueSize(String key) { IniSection section = getSection(); return section == null ? 0 : section.size(key); }
/** * Output the Ini to the given Writer. * * @param out the Writer to which this Ini should be written */ private void save(Writer out) { PrintWriter writer = null; if (out instanceof PrintWriter) { writer = (PrintWriter) out; } else { writer = new PrintWriter(out); } for (String sectionName : list) { IniSection section = doGetSection(sectionName); section.save(writer); } }
/** * Get the first value for the key in the first section. * * @param key the key * @return the value at the specified index * @throws ArrayIndexOutOfBoundsException when the index is out of bounds */ public String getValue(String key) { IniSection section = getSection(); return section == null ? null : section.get(key); }
/** * Add a value for the key. Duplicate values are not allowed. * * @param key the key for the section * @param value the value for the key * @return whether the value was added or is already present. */ public boolean addValue(String key, String value) { IniSection section = getSection(); return section == null || section.add(key, value); }
/** * Replace a value for a key. * A null for key or value is not allowed. * An empty string for a key is not allowed. * * @param key the key for the section * @param value the value for the key * @return {@code true} if the element was added or already was present */ public boolean replaceValue(String key, String value) { IniSection section = getSection(); return section == null || section.replace(key, value); }