001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022import java.io.Reader;
023import java.io.Writer;
024import java.util.Properties;
025
026/**
027 * Default implementation of the {@link PropertiesPersister} interface.
028 * Follows the native parsing of {@code java.util.Properties}.
029 *
030 * <p>Allows for reading from any Reader and writing to any Writer, for example
031 * to specify a charset for a properties file. This is a capability that standard
032 * {@code java.util.Properties} unfortunately lacked up until JDK 5:
033 * You were only able to load files using the ISO-8859-1 charset there.
034 *
035 * <p>Loading from and storing to a stream delegates to {@code Properties.load}
036 * and {@code Properties.store}, respectively, to be fully compatible with
037 * the Unicode conversion as implemented by the JDK Properties class. As of JDK 6,
038 * {@code Properties.load/store} will also be used for readers/writers,
039 * effectively turning this class into a plain backwards compatibility adapter.
040 *
041 * <p>The persistence code that works with Reader/Writer follows the JDK's parsing
042 * strategy but does not implement Unicode conversion, because the Reader/Writer
043 * should already apply proper decoding/encoding of characters. If you use prefer
044 * to escape unicode characters in your properties files, do <i>not</i> specify
045 * an encoding for a Reader/Writer (like ReloadableResourceBundleMessageSource's
046 * "defaultEncoding" and "fileEncodings" properties).
047 *
048 * @author Juergen Hoeller
049 * @since 10.03.2004
050 * @see java.util.Properties
051 * @see java.util.Properties#load
052 * @see java.util.Properties#store
053 */
054public class DefaultPropertiesPersister implements PropertiesPersister {
055
056        @Override
057        public void load(Properties props, InputStream is) throws IOException {
058                props.load(is);
059        }
060
061        @Override
062        public void load(Properties props, Reader reader) throws IOException {
063                props.load(reader);
064        }
065
066        @Override
067        public void store(Properties props, OutputStream os, String header) throws IOException {
068                props.store(os, header);
069        }
070
071        @Override
072        public void store(Properties props, Writer writer, String header) throws IOException {
073                props.store(writer, header);
074        }
075
076        @Override
077        public void loadFromXml(Properties props, InputStream is) throws IOException {
078                props.loadFromXML(is);
079        }
080
081        @Override
082        public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
083                props.storeToXML(os, header);
084        }
085
086        @Override
087        public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
088                props.storeToXML(os, header, encoding);
089        }
090
091}