@Override public InputStream openStream(URI uri) { try { return downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(); } catch (IOException e) { throw failToDownload(uri, e); } }
@Override public byte[] get() { byte[] buffer = new byte[15]; int sequenceId = getSequenceId(); initBase(buffer, sequenceId); return super.generate(buffer, sequenceId); } }
public static String slugify(String s) { Slug slug = new Slug(s); return slug .normalize() .removeNonAsciiChars() .dashifyNonWordChars() .dashifyWhitespaceChars() .collapseDashes() .removeHeadingDash() .removeTrailingDash() .toLowerCase(); }
@Override public void download(URI uri, File toFile) { try { copyInputStreamToFile(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(), toFile); } catch (IOException e) { deleteQuietly(toFile); throw failToDownload(uri, e); } }
@Override public boolean hasNext() { // Optimization to not call bufferNext() when already closed if (isClosed) { return false; } boolean hasNext = nextObject != null || bufferNext() != null; if (!hasNext) { close(); } return hasNext; }
@Override protected void doClose() { delegate.close(); } }
/** * Trust all certificates */ private static SSLSocketFactory createSocketFactory(Ssl context) { try { return context.newFactory(new AlwaysTrustManager()); } catch (Exception e) { throw new IllegalStateException("Fail to build SSL factory", e); } }
@Override public UuidGenerator.WithFixedBase createNew() { return new UuidGeneratorImpl().withFixedBase(); } }
/** * Create a universally unique identifier. It's recommended to use the non-static way * through {@link UuidFactory} which is available in IoC container. * @see UuidFactory#create() */ public static String create() { return UuidFactoryImpl.INSTANCE.create(); }
@Override public String generateKeyFrom(String source) { return slugify(source); } }
/** * Reads a stream of messages. This method returns an empty iterator if there are no messages. An * exception is raised on IO error or if messages have a different type than {@code parser}. * <p> * The stream is not closed by this method. It is closed when {@link CloseableIterator} traverses * all messages or when {@link CloseableIterator#close()} is called. * </p> */ public static <MSG extends Message> CloseableIterator<MSG> readStream(InputStream input, Parser<MSG> parser) { // the stream is closed by the CloseableIterator return new StreamIterator<>(parser, input); }
/** * Wraps a {@code CloseableIterator} and optionally other instances of {@code AutoCloseable} that must be closed * at the same time. The wrapped iterator is closed first then the other {@code AutoCloseable} in the defined order. * * @throws IllegalArgumentException if the parameter {@code otherCloseables} contains the wrapped iterator */ public static <T> CloseableIterator<T> wrap(CloseableIterator<T> iterator, AutoCloseable... otherCloseables) { return new CloseablesIteratorWrapper<>(iterator, otherCloseables); }
protected byte[] generate(byte[] buffer, int increment) { // Sequence number adds 3 bytes putLong(buffer, increment, 12, 3); return buffer; }
@Test(expected = IllegalArgumentException.class) public void wrap_fails_if_iterator_declared_in_other_closeables() { CloseableIterator iterator = new SimpleCloseableIterator(); CloseableIterator.wrap(iterator, iterator); }
@Override public void close() { for (CloseableIterator<?> reportIterator : closeables) { reportIterator.close(); } }
@Override protected String readString(URI uri, Charset charset) { try { return IOUtils.toString(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(), charset); } catch (IOException e) { throw failToDownload(uri, e); } }
@Override public byte[] download(URI uri) { try { return ByteStreams.toByteArray(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput()); } catch (IOException e) { throw failToDownload(uri, e); } }