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 try (FileInputStream in = new FileInputStream(source); // 052 FileOutputStream out = new FileOutputStream(target)) { 053 copyContent(in, out); 054 } 055 } 056 } 057 058 public static void copyContent(InputStream is, OutputStream out) throws IOException { 059 int data; 060 while (is.available() > 0 && (data = is.read()) != -1) { 061 out.write(data); 062 } 063 } 064 065 public static void appendResourceBundleFragments(String name, List<File> files, File target) throws IOException { 066 File l10n = new File(target, name); 067 File backup = new File(target, name + "~bak"); 068 if (!backup.exists()) { 069 backup.createNewFile(); 070 try (FileInputStream in = new FileInputStream(l10n); // 071 FileOutputStream out = new FileOutputStream(backup)) { 072 copyContent(in, out); 073 } 074 } 075 try (FileInputStream in = new FileInputStream(backup); // 076 FileOutputStream out = new FileOutputStream(l10n)) { 077 copyContent(in, out); 078 } 079 for (File file : files) { 080 try (InputStream in = new FileInputStream(file); // 081 OutputStream out = new FileOutputStream(l10n, true)) { // append 082 copyContent(in, out); 083 } 084 } 085 } 086 087}