public static void copy(InputStream input, File target) throws IOException {
		if (target.exists()) {
			throw new IOException("File already exists!");
		}

		File parentDir = target.getParentFile();

		if (!parentDir.exists()) {
			if (!parentDir.mkdirs()) {
				throw new IOException("Failed at creating directories!");
			}
		}

		if (!parentDir.isDirectory()) {
			throw new IOException("The parent of this file is no directory!?");
		}

		if (!target.createNewFile()) {
			throw new IOException("Failed at creating new empty file!");
		}

		if (input == null) {
			throw new NullPointerException("Input is null!");
		}

		byte[] buffer = new byte[1024];

		OutputStream output = new FileOutputStream(target);

		int realLength;

		while (input != null && (realLength = input.read(buffer)) > 0) {
			output.write(buffer, 0, realLength);
		}

		output.flush();
		output.close();
	}