Here you can find the source of copyStream(InputStream in, OutputStream out, String end)
public static void copyStream(InputStream in, OutputStream out, String end) throws Exception, IOException
//package com.java2s; //License from project: Mozilla Public License import java.io.*; public class Main { public static void copyStream(InputStream in, OutputStream out, String end) throws Exception, IOException { if (!estDefinit(end)) throw new Exception("End string can not be null or empty"); int i = -1, pos = 0; while (-1 != (i = in.read())) { out.write(i);//from www.j av a 2 s.c o m pos = i == end.charAt(pos) ? pos + 1 : 0; if (pos == end.length()) break; } } public static boolean estDefinit(Object obj) { if (null == obj) return false; else if (obj.equals("")) return false; return true; } }