@Override public void close() { source.close(); } };
@Override public void close() { delegate.close(); } }
@Override public void close() { delegate.close(); }
@Override public void close() { nextItem = null; try { clearGroup(); } finally { baseStream.close(); } }
@Override public void close() { Throwable error = null; if (current != null) { try { current.close(); } catch (Throwable th) { // NOSONAR We are managing errors error = th; } current = null; } while (streamIter.hasNext()) { ObjectStream<? extends T> cur = streamIter.next(); try { cur.close(); } catch (Throwable th) { // NOSONAR We are managing errors if (error == null) { error = th; } else { error.addSuppressed(th); } } } if (error != null) { throw Throwables.propagate(error); } } }
/** * Sort an object stream. This reads the original object stream into a list, sorts it, and * returns a new object stream backed by the list (after closing the original object stream). * * @param <T> The type of value in the object stream. * @param objectStream The object stream to sort. * @param comp The comparator to use to sort the object stream. * @return An object stream iterating over the sorted results. * @deprecated Use {@link ObjectStream#sorted(Comparator)}, possibly with {@link #wrap(Stream, Closeable)}. */ @Deprecated public static <T> ObjectStream<T> sort(@WillClose ObjectStream<T> objectStream, Comparator<? super T> comp) { ArrayList<T> list; try { list = Lists.newArrayList(objectStream); } finally { objectStream.close(); } Collections.sort(list, comp); return wrap(list); }
/** * Read an object stream into a list, closing when it is finished. * * @param <T> The type of item in the object stream. * @param objectStream The object stream. * @return A new list containing the elements of the object stream. * @deprecated Use {@link ObjectStream#collect(Collector)}. */ @SuppressWarnings("PMD.LooseCoupling") @Deprecated public static <T> List<T> makeList(@WillClose ObjectStream<? extends T> objectStream) { List<T> result = null; try { if (objectStream instanceof IteratorObjectStream) { result = ((IteratorObjectStream) objectStream).getList(); } if (result == null) { ImmutableList.Builder<T> builder = ImmutableList.builder(); builder.addAll(objectStream); result = builder.build(); } } finally { objectStream.close(); } return result; }
/** * Open a stream to read entities from this source. * @return A stream of entities. */ @Override public ObjectStream<Entity> openStream() throws IOException { BufferedReader reader = source.openBufferedStream(); ObjectStream<String> lines = new LineStream(reader); int headerLines = format.getHeaderLines(); List<String> header = new ArrayList<>(); while (header.size() < headerLines) { String line = lines.readObject(); if (line == null) { IOException ex = new IOException(String.format("%s: expected %d header lines, found %d", sourceURL, headerLines, header.size())); try { lines.close(); } catch (Throwable th) { ex.addSuppressed(th); } throw ex; } header.add(line); } LineEntityParser parser = format.makeParser(header); return ObjectStreams.transform(lines, parser); }
/** * Count the items in a stream. * * @param objectStream The object stream. * @return The number of items in the stream. * @deprecated Use {@link ObjectStream#collect(Collector)}. */ @SuppressWarnings("PMD.LooseCoupling") @Deprecated public static int count(@WillClose ObjectStream<?> objectStream) { try { if (objectStream instanceof IteratorObjectStream) { List<?> list = ((IteratorObjectStream) objectStream).getList(); if (list != null) { return list.size(); } } int n = 0; Object obj = objectStream.readObject(); while (obj != null) { n++; obj = objectStream.readObject(); } return n; } finally { objectStream.close(); } }
@Test public void testWrapIterator() { ObjectStream<String> stream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar").iterator()); try { // since collection wrapping tested general capabilities, these tests will be terser assertThat(stream, contains("foo", "bar")); } finally { stream.close(); } } //endregion
@Test public void testEmptyStreamIterable() { ObjectStream<String> cur = ObjectStreams.empty(); try { assertThat(Iterables.isEmpty(cur), equalTo(true)); } finally { cur.close(); } } //endregion
@Test public void testWrapCollectionIterator() { ObjectStream<String> objectStream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar")); try { List<String> strs = Lists.newArrayList(objectStream.iterator()); assertThat(strs, hasSize(2)); assertThat(strs, contains("foo", "bar")); } finally { objectStream.close(); } }
return ObjectStreams.wrap(ord.immutableSortedCopy(stream)); } finally { stream.close();
@Test public void testWrapEmptyCollection() { ObjectStream<?> objectStream = ObjectStreams.wrap(Collections.emptyList()); try { assertThat(objectStream.readObject(), nullValue()); try { objectStream.iterator().next(); fail("next should fail on empty stream"); } catch (NoSuchElementException e) { /* expected */ } } finally { objectStream.close(); } }
@Test public void testWrapCollection() { ObjectStream<String> objectStream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar")); try { assertThat(objectStream.readObject(), equalTo("foo")); assertThat(objectStream.readObject(), equalTo("bar")); assertThat(objectStream.readObject(), nullValue()); } finally { objectStream.close(); } }
@Test public void testEmptyStream() { ObjectStream<String> cur = ObjectStreams.empty(); try { assertThat(cur.readObject(), nullValue()); try { cur.iterator().next(); fail("next() on empty stream should fail"); } catch (NoSuchElementException e) { /* expected */ } } finally { cur.close(); } }
@Override public void close() { source.close(); } };
@Override public void close() { delegate.close(); } }
@Override public void close() { delegate.close(); }
@Override public void close() { nextItem = null; try { clearGroup(); } finally { baseStream.close(); } }