use of java.io.UncheckedIOException in project core-ng-project by neowu.
the class RedisImpl method setIfAbsent.
@Override
public boolean setIfAbsent(String key, String value, Duration expiration) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = pool.borrowItem();
try {
RedisConnection connection = item.resource;
connection.write(Protocol.Command.SET, encode(key), encode(value), NX, EX, encode(expiration.getSeconds()));
String result = connection.readSimpleString();
return "OK".equals(result);
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, 0, 1);
logger.debug("setIfAbsent, key={}, value={}, expiration={}, elapsedTime={}", key, value, expiration, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of java.io.UncheckedIOException in project core-ng-project by neowu.
the class RedisSetImpl method add.
@Override
public boolean add(String key, String value) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = redis.pool.borrowItem();
try {
RedisConnection connection = item.resource;
connection.write(SADD, encode(key), encode(value));
return connection.readLong() == 1;
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
redis.pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, 0, 1);
logger.debug("sadd, key={}, value={}, elapsedTime={}", key, value, elapsedTime);
redis.checkSlowOperation(elapsedTime);
}
}
use of java.io.UncheckedIOException in project core-ng-project by neowu.
the class FileBody method send.
@Override
public void send(Sender sender, ResponseHandlerContext context) {
logger.debug("[response] file={}", path);
try {
FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
sender.transferFrom(channel, new FileBodyCallback(channel));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of java.io.UncheckedIOException in project core-ng-project by neowu.
the class InputStreams method bytesWithExpectedLength.
public static byte[] bytesWithExpectedLength(InputStream stream, int expectedLength) {
byte[] bytes = new byte[expectedLength];
int position = 0;
try {
while (position < expectedLength) {
int bytesRead = stream.read(bytes, position, expectedLength - position);
if (bytesRead < 0)
break;
position += bytesRead;
}
if (stream.read() != -1)
throw new IOException(Strings.format("stream exceeds expected length, expected={}", expectedLength));
if (expectedLength != position)
throw new IOException(Strings.format("stream ends prematurely, expected={}, actual={}", expectedLength, position));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return bytes;
}
use of java.io.UncheckedIOException in project core-ng-project by neowu.
the class ClasspathResources method bytes.
public static byte[] bytes(String path) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resource = loader.getResource(path);
if (resource == null)
throw Exceptions.error("can not load resource, path={}", path);
URLConnection connection;
int length;
try {
connection = resource.openConnection();
length = connection.getContentLength();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (length <= 0) {
throw Exceptions.error("unexpected length of classpath resource, path={}, length={}", path, length);
}
try (InputStream stream = connection.getInputStream()) {
return InputStreams.bytesWithExpectedLength(stream, length);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations