Sync with L2JServer Jan 9th 2015.
This commit is contained in:
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2015 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Prevent the underlying input stream to close.
|
||||
* @author Joe Cheng, Zoey76
|
||||
*/
|
||||
public class CloseShieldedInputStream extends InputStream
|
||||
{
|
||||
private InputStream _in = null;
|
||||
|
||||
/**
|
||||
* Instantiates a new close shielded input stream.
|
||||
* @param in the in
|
||||
*/
|
||||
public CloseShieldedInputStream(InputStream in)
|
||||
{
|
||||
_in = in;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
_in = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
throw new IOException("Stream is null!");
|
||||
}
|
||||
return _in.read();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int read(byte b[]) throws IOException
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
throw new IOException("Stream is null!");
|
||||
}
|
||||
return _in.read(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int read(byte b[], int off, int len) throws IOException
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
throw new IOException("Stream is null!");
|
||||
}
|
||||
return _in.read(b, off, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public long skip(long n) throws IOException
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
throw new IOException("Stream is null!");
|
||||
}
|
||||
return _in.skip(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void mark(int readlimit)
|
||||
{
|
||||
if (_in != null)
|
||||
{
|
||||
_in.mark(readlimit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean markSupported()
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _in.markSupported();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void reset() throws IOException
|
||||
{
|
||||
if (_in == null)
|
||||
{
|
||||
throw new IOException("Stream is null!");
|
||||
}
|
||||
_in.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the underlying stream.
|
||||
* @return the underlying stream
|
||||
*/
|
||||
public InputStream getUnderlyingStream()
|
||||
{
|
||||
return _in;
|
||||
}
|
||||
}
|
@@ -22,6 +22,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.Duration;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -242,4 +243,34 @@ public final class PropertiesParser
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param durationPattern
|
||||
* @param defaultValue
|
||||
* @return {@link Duration} object by the durationPattern specified, {@code null} in case of malformed pattern.
|
||||
*/
|
||||
public Duration getDuration(String durationPattern, String defaultValue)
|
||||
{
|
||||
return getDuration(durationPattern, defaultValue, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param durationPattern
|
||||
* @param defaultValue
|
||||
* @param defaultDuration
|
||||
* @return {@link Duration} object by the durationPattern specified, the defaultDuration in case of malformed pattern.
|
||||
*/
|
||||
public Duration getDuration(String durationPattern, String defaultValue, Duration defaultDuration)
|
||||
{
|
||||
final String value = getString(durationPattern, defaultValue);
|
||||
try
|
||||
{
|
||||
return TimeUtil.parseDuration(value);
|
||||
}
|
||||
catch (IllegalStateException e)
|
||||
{
|
||||
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + durationPattern + " specified value: " + value + " should be time patttern using default value: " + defaultValue);
|
||||
}
|
||||
return defaultDuration;
|
||||
}
|
||||
}
|
||||
|
127
trunk/java/com/l2jserver/util/TimeUtil.java
Normal file
127
trunk/java/com/l2jserver/util/TimeUtil.java
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2015 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public class TimeUtil
|
||||
{
|
||||
public static int findIndexOfNonDigit(CharSequence text)
|
||||
{
|
||||
for (int i = 0; i < text.length(); i++)
|
||||
{
|
||||
if (Character.isDigit(text.charAt(i)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses patterns like:
|
||||
* <ul>
|
||||
* <li>1min or 10mins</li>
|
||||
* <li>1day or 10days</li>
|
||||
* <li>1week or 4weeks</li>
|
||||
* <li>1month or 12months</li>
|
||||
* <li>1year or 5years</li>
|
||||
* </ul>
|
||||
* @param datePattern
|
||||
* @return {@link Duration} object converted by the date pattern specified.
|
||||
* @throws IllegalStateException when malformed pattern specified.
|
||||
*/
|
||||
public static Duration parseDuration(String datePattern)
|
||||
{
|
||||
final int index = findIndexOfNonDigit(datePattern);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect time format given: " + datePattern);
|
||||
}
|
||||
try
|
||||
{
|
||||
int val = Integer.parseInt(datePattern.substring(0, index));
|
||||
final String type = datePattern.substring(index);
|
||||
final ChronoUnit unit;
|
||||
switch (type.toLowerCase())
|
||||
{
|
||||
case "sec":
|
||||
case "secs":
|
||||
{
|
||||
unit = ChronoUnit.SECONDS;
|
||||
break;
|
||||
}
|
||||
case "min":
|
||||
case "mins":
|
||||
{
|
||||
unit = ChronoUnit.MINUTES;
|
||||
break;
|
||||
}
|
||||
case "hour":
|
||||
case "hours":
|
||||
{
|
||||
unit = ChronoUnit.HOURS;
|
||||
break;
|
||||
}
|
||||
case "day":
|
||||
case "days":
|
||||
{
|
||||
unit = ChronoUnit.DAYS;
|
||||
break;
|
||||
}
|
||||
case "week":
|
||||
case "weeks":
|
||||
{
|
||||
unit = ChronoUnit.WEEKS;
|
||||
break;
|
||||
}
|
||||
case "month":
|
||||
case "months":
|
||||
{
|
||||
unit = ChronoUnit.MONTHS;
|
||||
break;
|
||||
}
|
||||
case "year":
|
||||
case "years":
|
||||
{
|
||||
unit = ChronoUnit.YEARS;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
unit = ChronoUnit.valueOf(type);
|
||||
if (unit == null)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect format: " + type + " !!");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Duration.of(val, unit);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect time format given: " + datePattern + " val: " + datePattern.substring(0, index));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user