L2Properties.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.nio.charset.Charset;
  23. import java.util.Properties;
  24. import java.util.logging.Logger;
  25. /**
  26. * @author Noctarius
  27. */
  28. public final class L2Properties extends Properties
  29. {
  30. private static final long serialVersionUID = 1L;
  31. private static Logger _log = Logger.getLogger(L2Properties.class.getName());
  32. public L2Properties() { }
  33. public L2Properties(String name) throws IOException
  34. {
  35. load(new FileInputStream(name));
  36. }
  37. public L2Properties(File file) throws IOException
  38. {
  39. load(new FileInputStream(file));
  40. }
  41. public L2Properties(InputStream inStream) throws IOException
  42. {
  43. load(inStream);
  44. }
  45. public L2Properties(Reader reader) throws IOException
  46. {
  47. load(reader);
  48. }
  49. public void load(String name) throws IOException
  50. {
  51. load(new FileInputStream(name));
  52. }
  53. public void load(File file) throws IOException
  54. {
  55. load(new FileInputStream(file));
  56. }
  57. @Override
  58. public void load(InputStream inStream) throws IOException
  59. {
  60. InputStreamReader reader = null;
  61. try
  62. {
  63. reader = new InputStreamReader(inStream, Charset.defaultCharset());
  64. super.load(reader);
  65. }
  66. finally
  67. {
  68. inStream.close();
  69. if (reader != null)
  70. reader.close();
  71. }
  72. }
  73. @Override
  74. public void load(Reader reader) throws IOException
  75. {
  76. try
  77. {
  78. super.load(reader);
  79. }
  80. finally
  81. {
  82. reader.close();
  83. }
  84. }
  85. @Override
  86. public String getProperty(String key)
  87. {
  88. String property = super.getProperty(key);
  89. if (property == null)
  90. {
  91. _log.info("L2Properties: Missing property for key - " + key);
  92. return null;
  93. }
  94. return property.trim();
  95. }
  96. @Override
  97. public String getProperty(String key, String defaultValue)
  98. {
  99. String property = super.getProperty(key, defaultValue);
  100. if (property == null)
  101. {
  102. _log.warning("L2Properties: Missing defaultValue for key - " + key);
  103. return null;
  104. }
  105. return property.trim();
  106. }
  107. }