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

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

Introduction

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

Prototype


public <C extends Closeable> C register(@Nullable C closeable) 

Source Link

Document

Registers the given closeable to be closed when this Closer is #close closed .

Usage

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(String val) throws IOException {
    Closer closer = Closer.create();
    try {/*ww w  . j  a  va2  s.  com*/
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));
        if (val.length() > MAX_INDEXVAL_SIZE) {
            throw new IOException("KeyFactory - indexValue max 127 chars");
        }
        w.write(INDEXVAL_STRING); //string
        w.write(val.getBytes("UTF8"));
        w.write(STRING_END); //stop
        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static Key getIndexKeyByte(ByteId dbId, ByteId typeId, ByteId indexId, byte[] indexValue,
        ByteId objNrId) throws IOException {
    Closer closer = Closer.create();
    try {//from  w  w w  . ja va  2s .c o  m
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        w.write(dbId.getId());
        w.writeByte(indexKF);
        w.write(typeId.getId());
        w.write(indexId.getId());
        w.write(indexValue);
        w.write(objNrId.getId());
        w.flush();

        return new Key(baos.toByteArray());
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static String deserializeLexicoString(DataInputStream r) throws IOException {
    Closer closer = Closer.create();
    try {/*  w  w w .ja v a  2  s .  com*/
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        do {
            byte b = r.readByte();
            if (b == STRING_END) {
                break;
            }
            baos.write(b);
        } while (true);
        w.flush();

        return new String(baos.toByteArray(), "UTF8");
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(int val) throws IOException {
    Closer closer = Closer.create();
    try {// w  w  w  .j  av a  2 s. c  o m
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        byte sign = (val < 0 ? (byte) 0 : (byte) 1);
        int absVal = Math.abs(val);
        byte[] iVal = Ints.toByteArray(absVal);

        w.write(INDEXVAL_INT);
        w.write(sign);
        w.write(iVal);

        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(long val) throws IOException {
    Closer closer = Closer.create();
    try {/*from  w  ww .j av  a 2  s . c  o  m*/
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        byte sign = (val < 0 ? (byte) 0 : (byte) 1);
        long absVal = Math.abs(val);
        byte[] iVal = Longs.toByteArray(absVal);

        w.write(INDEXVAL_LONG);
        w.write(sign);
        w.write(iVal);

        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

public static DMKeyComponents getKeyComponents(Key key) throws IOException {
    Closer closer = Closer.create();
    try {//from  ww w. j  a va  2  s  .  co  m
        ByteArrayInputStream bais = closer.register(new ByteArrayInputStream(key.getArray()));
        DataInputStream r = closer.register(new DataInputStream(bais));

        ByteId dbId = deserializeByteId(r);
        byte keyType = r.readByte();
        ByteId typeId = deserializeByteId(r);

        if (keyType == DMKeyFactory.typeKF) {
            return new TypeKeyComp(dbId, typeId);
            //            } else if (keyType == DMKeyFactory.tmFieldKF) {
            //                readTMFieldKey(r, dbId, typeId);
        } else if (keyType == DMKeyFactory.dataKF) {
            return readDataKey(r, dbId, typeId);
        } else if (keyType == DMKeyFactory.indexKF) {
            return readIndexKey(r, dbId, typeId);
        } else {
            throw new IOException("getKeyComponents - unknown type of key");
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.google.devtools.build.android.desugar.Desugar.java

/**
 * Transform a list of Path to a list of InputFileProvider and register them with the given
 * closer.//from  w  w  w . ja  v  a  2  s.c  o  m
 */
@SuppressWarnings("MustBeClosedChecker")
private static ImmutableList<InputFileProvider> toRegisteredInputFileProvider(Closer closer, List<Path> paths)
        throws IOException {
    ImmutableList.Builder<InputFileProvider> builder = new ImmutableList.Builder<>();
    for (Path path : paths) {
        builder.add(closer.register(toInputFileProvider(path)));
    }
    return builder.build();
}

From source file:org.glowroot.local.ui.ClasspathCache.java

private static void loadClassNamesFromJarFile(File jarFile, Multimap<String, File> newClassNameLocations)
        throws IOException {
    Closer closer = Closer.create();
    InputStream s = new FileInputStream(jarFile);
    JarInputStream jarIn = closer.register(new JarInputStream(s));
    try {/*from  w  w  w . j  a va 2s  . c o  m*/
        loadClassNamesFromManifestClassPath(jarIn, jarFile, newClassNameLocations);
        loadClassNamesFromJarInputStream(jarIn, jarFile, newClassNameLocations);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.util.JobLauncherUtils.java

private static ParallelRunner getParallelRunner(FileSystem fs, Closer closer, int parallelRunnerThreads,
        Map<String, ParallelRunner> parallelRunners) {
    String uriAndHomeDir = new Path(new Path(fs.getUri()), fs.getHomeDirectory()).toString();
    if (!parallelRunners.containsKey(uriAndHomeDir)) {
        parallelRunners.put(uriAndHomeDir, closer.register(new ParallelRunner(parallelRunnerThreads, fs)));
    }/*from w  w w . ja v a 2 s  .c  om*/
    return parallelRunners.get(uriAndHomeDir);
}

From source file:org.apache.jackrabbit.oak.run.SegmentUtils.java

private static FileStore bootstrapFileStore(String path, Closer closer) throws IOException {
    return closer.register(bootstrapFileStore(path));
}