Search in sources :

Example 86 with UncheckedIOException

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 87 with UncheckedIOException

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 88 with UncheckedIOException

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);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 89 with UncheckedIOException

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;
}
Also used : UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 90 with UncheckedIOException

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);
    }
}
Also used : InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)826 IOException (java.io.IOException)786 File (java.io.File)109 Path (java.nio.file.Path)106 ArrayList (java.util.ArrayList)70 InputStream (java.io.InputStream)58 Map (java.util.Map)58 List (java.util.List)55 HashMap (java.util.HashMap)44 Test (org.junit.Test)38 Files (java.nio.file.Files)37 Collectors (java.util.stream.Collectors)37 Stream (java.util.stream.Stream)31 URL (java.net.URL)29 StringWriter (java.io.StringWriter)27 CursorContext (org.neo4j.io.pagecache.context.CursorContext)25 FileInputStream (java.io.FileInputStream)23 HashSet (java.util.HashSet)23 PageCursor (org.neo4j.io.pagecache.PageCursor)22 Optional (java.util.Optional)21