List of usage examples for com.google.common.io ByteSource read
public byte[] read() throws IOException
From source file:at.ac.univie.isc.asio.io.Payload.java
/** * Copy the content from given {@code ByteSource} to an array. * * @param source source of binary data/*from w w w . j a v a 2s . c om*/ * @return byte array containing the contents of the {@code source} * @throw UncheckedIOException if reading the source fails */ public static byte[] asArray(final ByteSource source) { try { return source.read(); } catch (IOException e) { throw new Unchecked.UncheckedIOException(e); } }
From source file:com.epam.reportportal.utils.files.ImageConverter.java
public static ByteSource convertIfImage(ByteSource content) { try {/* w ww. j a v a 2s. co m*/ byte[] data = content.read(); if (isImage(data)) { return convert(data); } else { return ByteSource.wrap(data); } } catch (IOException e) { throw new InternalReportPortalClientException("Unable to read screenshot file. " + e); } }
From source file:org.isisaddons.module.fakedata.dom.IsisBlobs.java
private static Blob asBlob(final String fileName) { final URL resource = Resources.getResource(IsisBlobs.class, "blobs/" + fileName); final ByteSource byteSource = Resources.asByteSource(resource); final byte[] bytes; try {/*from w w w. j av a 2 s . com*/ bytes = byteSource.read(); return new Blob(fileName, mimeTypeFor(fileName), bytes); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:net.derquinse.bocas.BocasExerciser.java
public static void check(ByteSource value, ByteSource data) throws IOException { assertEquals(data.read(), value.read()); }
From source file:com.opengamma.strata.collect.io.ArrayByteSource.java
/** * Creates an instance from another byte source. * /*w w w .j a va 2 s. c o m*/ * @param other the other byte source * @return the byte source * @throws UncheckedIOException if an IO error occurs */ public static ArrayByteSource from(ByteSource other) { if (other instanceof ArrayByteSource) { return (ArrayByteSource) other; } return new ArrayByteSource(Unchecked.wrap(() -> other.read())); }
From source file:net.derquinse.bocas.jdbc.JDBCBocasDialect.java
/** Puts a value in a prepared statement parameter. */ void setValue(PreparedStatement ps, int index, ByteSource value) throws SQLException { try {/*from w ww .ja va 2s . c o m*/ ps.setBytes(index, value.read()); } catch (IOException e) { throw new SQLException(e); } }
From source file:at.ac.univie.isc.asio.flock.FlockAssembler.java
private FlockConfig parse(final ByteSource source) { try {//from w w w .j a v a2s .c om return jackson.readValue(source.read(), FlockConfig.class); } catch (IOException e) { throw new InvalidFlockConfiguration(e); } }
From source file:org.isisaddons.wicket.pdfjs.fixture.scripts.data.DemoObjectsFixture.java
private Blob asBlob(final String documentName, final String urlStr) { try {//from w w w .j a v a 2 s . co m final URL url = new URL(urlStr); final HttpURLConnection httpConn = openConnection(url); final String contentType = httpConn.getContentType(); final MimeType mimeType = determineMimeType(contentType); httpConn.disconnect(); final ByteSource byteSource = Resources.asByteSource(url); final byte[] bytes = byteSource.read(); return new Blob(documentName, mimeType.getBaseType(), bytes); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.jasig.cas.ticket.registry.encrypt.EncodedTicket.java
/** * Creates a new encoded ticket using the given encoder to encode the given * source ticket.//ww w . ja va 2 s . co m * * @param encodedTicket the encoded ticket * @param encodedTicketId the encoded ticket id */ public EncodedTicket(final ByteSource encodedTicket, final String encodedTicketId) { try { this.id = encodedTicketId; this.encodedTicket = encodedTicket.read(); } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:com.pinterest.pinlater.commons.config.ConfigFileWatcher.java
/** * Adds a watch on the specified file. The file must exist, otherwise a FileNotFoundException * is returned. If the file is deleted after a watch is established, the watcher will log errors * but continue to monitor it, and resume watching if it is recreated. * * @param filePath path to the file to watch. * @param onUpdate function to call when a change is detected to the file. The entire contents * of the file will be passed in to the function. Note that onUpdate will be * called once before this call completes, which facilities initial load of data. * This callback is executed synchronously on the watcher thread - it is * important that the function be non-blocking. *//*from w ww. j a va 2 s .c o m*/ public synchronized void addWatch(String filePath, Function<byte[], Void> onUpdate) throws IOException { MorePreconditions.checkNotBlank(filePath); Preconditions.checkNotNull(onUpdate); // Read the file and make the initial onUpdate call. File file = new File(filePath); ByteSource byteSource = Files.asByteSource(file); onUpdate.apply(byteSource.read()); // Add the file to our map if it isn't already there, and register the new change watcher. ConfigFileInfo configFileInfo = watchedFileMap.get(filePath); if (configFileInfo == null) { configFileInfo = new ConfigFileInfo(file.lastModified(), byteSource.hash(HASH_FUNCTION)); watchedFileMap.put(filePath, configFileInfo); } configFileInfo.changeWatchers.add(onUpdate); }