KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > SafeFileInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.io.*;
14
15 /**
16  * Given a target and a temporary locations, it tries to read the contents
17  * from the target. If a file does not exist at the target location, it tries
18  * to read the contents from the temporary location.
19  *
20  * @see SafeFileOutputStream
21  */

22 public class SafeFileInputStream extends FilterInputStream {
23     protected static final String JavaDoc EXTENSION = ".bak"; //$NON-NLS-1$
24

25     public SafeFileInputStream(File file) throws IOException {
26         this(file.getAbsolutePath(), null);
27     }
28
29     public SafeFileInputStream(String JavaDoc targetName) throws IOException {
30         this(targetName, null);
31     }
32
33     /**
34      * If targetPath is null, the file will be created in the default-temporary directory.
35      */

36     public SafeFileInputStream(String JavaDoc targetPath, String JavaDoc tempPath) throws IOException {
37         super(getInputStream(targetPath, tempPath));
38     }
39
40     private static InputStream getInputStream(String JavaDoc targetPath, String JavaDoc tempPath) throws IOException {
41         File target = new File(targetPath);
42         if (!target.exists()) {
43             if (tempPath == null)
44                 tempPath = target.getAbsolutePath() + EXTENSION;
45             target = new File(tempPath);
46         }
47         return new BufferedInputStream(new FileInputStream(target));
48     }
49 }
Popular Tags