All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.commons.StreamUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.teamscale.commons;

import java.io.IOException;
import java.io.OutputStream;

import org.conqat.lib.commons.function.ConsumerWithException;

import com.google.common.io.CountingOutputStream;

/**
 * Utils for {@link java.util.stream.Stream} types.
 */
public class StreamUtils {

	private StreamUtils() {
		// Avoid initialization of util class.
	}

	/**
	 * Applies the write operation while counting the number of bytes written into the content stream.
	 *
	 * @param content
	 *            Content written to the given {@code writeStream}
	 * @param writeStream
	 *            Consumer which accepts the data to be written
	 * @return Number of bytes passed to {@code writeStream}
	 */
	public static long writeWithByteCounter(OutputStream content,
			ConsumerWithException writeStream) throws IOException {
		try (CountingOutputStream counter = new CountingOutputStream(content)) {
			writeStream.accept(counter);
			return counter.getCount();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy