1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.core.internal.preferences; 15 16 import java.io.*; 17 18 /** 19 * Given a target and a temporary locations, it tries to read the contents 20 * from the target. If a file does not exist at the target location, it tries 21 * to read the contents from the temporary location. 22 * This class handles buffering of output stream contents. 23 * 24 * Copied from org.eclipse.core.internal.localstore.SafeFileOutputStream 25 * 26 * @see SafeFileOutputStream 27 */ 28 public class SafeFileInputStream extends FilterInputStream { 29 protected static final String EXTENSION = ".bak"; //$NON-NLS-1$ 30 SafeFileInputStream(File file)31 public SafeFileInputStream(File file) throws IOException { 32 this(file.getAbsolutePath(), null); 33 } 34 35 /** 36 * If targetPath is null, the file will be created in the default-temporary directory. 37 */ SafeFileInputStream(String targetPath, String tempPath)38 public SafeFileInputStream(String targetPath, String tempPath) throws IOException { 39 super(getInputStream(targetPath, tempPath)); 40 } 41 getInputStream(String targetPath, String tempPath)42 private static InputStream getInputStream(String targetPath, String tempPath) throws IOException { 43 File target = new File(targetPath); 44 if (!target.exists()) { 45 if (tempPath == null) 46 tempPath = target.getAbsolutePath() + EXTENSION; 47 target = new File(tempPath); 48 } 49 return new BufferedInputStream(new FileInputStream(target)); 50 } 51 } 52