blob: acd9335c837eb14f4db0520f26c7a344ebd0ec6c [file] [log] [blame]
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.xmlbeans.impl.common;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.MalformedURLException;
public class IOUtil
{
public static void copyCompletely(InputStream input, OutputStream output)
throws IOException
{
byte[] buf = new byte[8192];
while (true)
{
int length = input.read(buf);
if (length < 0)
break;
output.write(buf, 0, length);
}
try { input.close(); } catch (IOException ignore) {}
try { output.close(); } catch (IOException ignore) {}
}
public static void copyCompletely(URL input, URL output)
throws IOException
{
try
{
InputStream in = input.openStream();
File out = new File(output.getPath());
File dir = out.getParentFile();
dir.mkdirs();
IOUtil.copyCompletely(in, new FileOutputStream(out));
}
catch (IllegalArgumentException e)
{
throw new IOException("Cannot copy to " + output);
}
}
public static URL fileToURL(File file)
{
try
{
return file.toURL();
}
catch (MalformedURLException e)
{
return null;
}
}
}