| package org.eclipse.epp.installer.internal.core; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| import java.text.MessageFormat; |
| |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.epp.installer.core.IActionConstants; |
| import org.eclipse.epp.installer.core.IErrorConstants; |
| import org.eclipse.epp.installer.core.model.Context; |
| |
| /** |
| */ |
| public class Util { |
| |
| private static final int BUFFER_SIZE = 4096; |
| |
| /** |
| * Method copyStreams. |
| * |
| * @param is |
| * InputStream |
| * @param os |
| * OutputStream |
| * @throws IOException |
| */ |
| public static void copyStreams(InputStream is, OutputStream os) |
| throws IOException { |
| byte[] buf = new byte[BUFFER_SIZE]; |
| int n; |
| while ((n = is.read(buf)) != -1) { |
| os.write(buf, 0, n); |
| } |
| os.flush(); |
| } |
| |
| /** |
| * Method copyFiles |
| * |
| * @param in |
| * File |
| * @param out |
| * File |
| * @throws IOException |
| */ |
| public static void copyFiles(File in, File out) throws IOException { |
| FileInputStream fis = new FileInputStream(in); |
| FileOutputStream fos = new FileOutputStream(out); |
| copyStreams(fis, fos); |
| fis.close(); |
| fos.close(); |
| } |
| |
| /** |
| * Method createFileFromStream. |
| * |
| * @param installer |
| * Context |
| * @param file |
| * File |
| * @param content |
| * InputStream |
| * @return IStatus |
| * @throws IOException |
| */ |
| public static IStatus createFileFromStream(Context installer, File file, |
| InputStream content, long lastModified) throws IOException { |
| if (file.exists() |
| && !installer.getOptions().isNoOverwriteWarningDuringInstall() |
| && !installer.confirm(IActionConstants.OVERWRITE_FILE, |
| "Overwrite file {0}?", new Object[] { file })) { |
| String message = MessageFormat.format("Cannot create file {0}.", |
| new Object[] { file }); |
| return new Status(IStatus.INFO, Context.PLUGIN_ID, |
| IErrorConstants.ERROR_USER_SKIPPED, message, null); |
| } |
| |
| IPath path = (new Path(file.getAbsolutePath())).removeLastSegments(1); |
| for (int i = 0; i <= path.segmentCount(); i++) { |
| IPath subPath = path.uptoSegment(i); |
| File dir = new File(subPath.toOSString()); |
| if (!dir.exists()) { |
| boolean created = dir.mkdir(); |
| if (created) { |
| installer.getInstallLog().addEntry( |
| new InstallLogEntry( |
| InstallLog.DIRECTORY_CREATED_ENTRY, dir |
| .getAbsolutePath())); |
| } else { |
| return new Status(IStatus.ERROR, Context.PLUGIN_ID, |
| "Failed to create directory " |
| + dir.getAbsolutePath()); |
| } |
| } |
| } |
| |
| OutputStream os = new FileOutputStream(file); |
| ; |
| try { |
| |
| copyStreams(content, os); |
| |
| installer.getInstallLog().addEntry( |
| new InstallLogEntry(InstallLog.FILE_CREATED_ENTRY, file |
| .getAbsolutePath())); |
| |
| } finally { |
| os.close(); |
| } |
| |
| // file.setLastModified(lastModified); |
| // Platform.getPlatform().setFileCreationTime(file.getAbsolutePath(),lastModified); |
| |
| return Status.OK_STATUS; |
| } |
| } |