001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Stephane Lacoin 018 */ 019package org.nuxeo.runtime.tomcat.dev; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.util.List; 028 029public class IOUtils { 030 031 public static void deleteTree(File dir) { 032 for (File file : dir.listFiles()) { 033 if (file.isDirectory()) { 034 deleteTree(file); 035 } else { 036 file.delete(); 037 } 038 } 039 dir.delete(); 040 } 041 042 public static void copyTree(File source, File target) throws IOException { 043 if (source.isDirectory()) { 044 if (!target.exists()) { 045 target.mkdir(); 046 } 047 for (File child : source.listFiles()) { 048 copyTree(child, new File(target, child.getName())); 049 } 050 } else { 051 copyContent(new FileInputStream(source), new FileOutputStream(target)); 052 } 053 } 054 055 public static void copyContent(InputStream is, OutputStream out) throws IOException { 056 int data; 057 while (is.available() > 0 && (data = is.read()) != -1) { 058 out.write(data); 059 } 060 } 061 062 public static void appendResourceBundleFragments(String name, List<File> files, File target) throws IOException { 063 File l10n = new File(target, name); 064 File backup = new File(target, name + "~bak"); 065 if (!backup.exists()) { 066 backup.createNewFile(); 067 IOUtils.copyContent(new FileInputStream(l10n), new FileOutputStream(backup)); 068 } 069 IOUtils.copyContent(new FileInputStream(backup), new FileOutputStream(l10n)); 070 for (File file : files) { 071 InputStream in = new FileInputStream(file); 072 OutputStream out = new FileOutputStream(l10n, true); 073 IOUtils.copyContent(in, out); 074 } 075 } 076 077}