001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2009 Sun Microsystems Inc. 015 * Portions Copyright 2010–2011 ApexIdentity Inc. 016 * Portions Copyright 2011-2014 ForgeRock AS. 017 */ 018 019package org.forgerock.openig.io; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.OutputStream; 024import java.io.Reader; 025import java.io.Writer; 026 027/** 028 * Utility class that can stream to and from streams. 029 */ 030public final class Streamer { 031 032 /** Size of buffer to use during streaming. */ 033 private static final int BUF_SIZE = 8 * 1024; 034 035 /** Static methods only. */ 036 private Streamer() { 037 } 038 039 /** 040 * Streams all data from an input stream to an output stream. 041 * 042 * @param in the input stream to stream the data from. 043 * @param out the output stream to stream the data to. 044 * @throws IOException if an I/O exception occurs. 045 */ 046 public static void stream(InputStream in, OutputStream out) throws IOException { 047 byte[] buf = new byte[BUF_SIZE]; 048 int n; 049 while ((n = in.read(buf, 0, BUF_SIZE)) != -1) { 050 out.write(buf, 0, n); 051 } 052 } 053 054 /** 055 * Streams data from an input stream to an output stream, up to a specified 056 * length. 057 * 058 * @param in the input stream to stream the data from. 059 * @param out the output stream to stream the data to. 060 * @param len the number of bytes to stream. 061 * @return the actual number of bytes streamed. 062 * @throws IOException if an I/O exception occurs. 063 */ 064 public static int stream(InputStream in, OutputStream out, int len) throws IOException { 065 int remaining = len; 066 byte[] buf = new byte[BUF_SIZE]; 067 int n; 068 while (remaining > 0 && (n = in.read(buf, 0, Math.min(remaining, BUF_SIZE))) >= 0) { 069 out.write(buf, 0, n); 070 remaining -= n; 071 } 072 return len - remaining; 073 } 074 075 /** 076 * Streams all characters from a reader to a writer. 077 * 078 * @param in reader to stream the characters from. 079 * @param out the writer to stream the characters to. 080 * @throws IOException if an I/O exception occurs. 081 */ 082 public static void stream(Reader in, Writer out) throws IOException { 083 char[] buf = new char[BUF_SIZE]; 084 int n; 085 while ((n = in.read(buf, 0, BUF_SIZE)) != -1) { 086 out.write(buf, 0, n); 087 } 088 } 089}