KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.core.internal.localstore;
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
private static final int DEFAUT_BUFFER_SIZE = 2048;
25
26     public SafeFileInputStream(File file) throws IOException {
27         this(file.getAbsolutePath(), null);
28     }
29
30     /**
31      * If targetPath is null, the file will be created in the default-temporary directory.
32      */

33     public SafeFileInputStream(String JavaDoc targetPath, String JavaDoc tempPath) throws IOException {
34         super(getInputStream(targetPath, tempPath, DEFAUT_BUFFER_SIZE));
35     }
36
37     /**
38      * If targetPath is null, the file will be created in the default-temporary directory.
39      */

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