Example usage for com.google.common.io Closer rethrow

List of usage examples for com.google.common.io Closer rethrow

Introduction

In this page you can find the example usage for com.google.common.io Closer rethrow.

Prototype

public RuntimeException rethrow(Throwable e) throws IOException 

Source Link

Document

Stores the given throwable and rethrows it.

Usage

From source file:org.apache.gobblin.metastore.FsStateStore.java

@Override
@SuppressWarnings("unchecked")
public T get(String storeName, String tableName, String stateId) throws IOException {
    Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName);
    if (!this.fs.exists(tablePath)) {
        return null;
    }//from w ww.  j  ava  2s.  c o m

    Closer closer = Closer.create();
    try {
        @SuppressWarnings("deprecation")
        GobblinSequenceFileReader reader = closer
                .register(new GobblinSequenceFileReader(this.fs, tablePath, this.conf));
        try {
            Text key = new Text();
            T state = this.stateClass.newInstance();
            while (reader.next(key)) {
                state = (T) reader.getCurrentValue(state);
                if (key.toString().equals(stateId)) {
                    return state;
                }
            }
        } catch (Exception e) {
            throw new IOException(
                    "failure retrieving state from storeName " + storeName + " tableName " + tableName, e);
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }

    return null;
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed or
 * flushed.//w ww . ja  va2 s  . c o m
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull @WillNotClose OutputStream to, long limit)
        throws IOException {
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed or
 * flushed./* w  w w.  ja  v a  2 s .  co m*/
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull PrimitiveSink to, long limit) throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. Byte processing stops on EOF
 * or when {@link ByteProcessor#processBytes(byte[], int, int) processBytes}
 * returns {@code false}.//w w w  . jav a2 s  . c  om
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull ByteProcessor<?> to, long limit) throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
public static byte[] toByteArray(@NonNull File file) throws IOException {
    final int size = Ints.checkedCast(file.length());
    Closer closer = Closer.create();
    try {/*from w w  w . j  a v  a2  s  .  com*/
        return toByteArray(closer.register(new FileInputStream(file)), size);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
public static boolean contentEquals(@NonNull ByteSource source, @NonNull @WillNotClose InputStream in)
        throws IOException {
    final Closer closer = Closer.create();
    try {/*w ww. j  av  a 2  s. co  m*/
        return contentEquals(closer.register(source.openStream()), in);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
public static boolean contentEquals(@NonNull ByteSource source1, @NonNull ByteBuffer buffer)
        throws IOException {
    final Closer closer = Closer.create();
    try {//from  ww w  . j a  v  a2s  .c  o  m
        return contentEquals(closer.register(source1.openStream()), buffer);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Used instead of {@link #toByteArray(ByteSource)} when the size of the
 * {@code ByteSource} is known and {@link ByteSource#size() size()} should
 * not be called on {@code source}.//from w ww. j a  v a  2s  .c o m
 */
@ThreadLocalArray(8192)
public static byte[] toByteArray(ByteSource source, int expectedSize) throws IOException {
    final Closer closer = Closer.create();
    try {
        return toByteArray(closer.register(source.openStream()), expectedSize);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
static boolean contentEqualsImpl(@NonNull ByteSource source, byte[] bytes, int off, int len)
        throws IOException {
    checkPositionIndexes(off, off + len, bytes.length);
    final Closer closer = Closer.create();
    try {/*w  w w.  j  a v  a2s  .c o m*/
        return contentEquals(closer.register(source.openStream()), bytes, off, len);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
static boolean contentEqualsImpl(@NonNull ByteSource source1, @NonNull ByteSource source2) throws IOException {
    final Closer closer = Closer.create();
    try {/* ww w  .ja v a 2s .c o m*/
        return contentEquals(closer.register(source1.openStream()), closer.register(source2.openStream()));
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}