⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 noreplacingoutputstream.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
字号:
package planet.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * This class abstracts an stream that is capable of no replace 
 * the new saved status, according a specified boolean flag.
 * If the file must not be replaced, a new file is created with
 * a filename of type "filename_x", where "filename" is the 
 * specified base filename, and "x" is an iterative version number.
 * That is, if "out" filename just exists, it attempts to create
 * a new file "out_1"; if this just exist, it attempts to create
 * a new file named "out_2"; ... 
 * @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
 * 11-jul-2005
 */
public class NoReplacingOutputStream extends OutputStream {
    /**
     * Shows the current incremental number of version to be used under no 
     * replacing context.
     */
    private static int version = 1;
	/**
	 * Shows if the file must be replaced with the new status to save. 
	 */
	private boolean replace;
	/**
	 * Shows the base filename that must save the new status.
	 */
	private String filename = null;
	/**
	 * FileOutputStream stream to save the bytes stream.
	 */
	private FileOutputStream fos = null;
	
	/**
	 * Constructor that initialize this stream with the specified base
	 * <b>filename</b>. The <b>replace</b> flag shows if the file must
	 * be replaced with new save status or not. If must not be replaced,
	 * a suffix "_x" is added to the filename, where "x" is the number of 
	 * version.
	 * @param filename Base filename to save new status.
	 * @param replace Boolean flag that shows if the file must be replaced.
	 * @throws FileNotFoundException if the new desired file just exists.
	 */
	public NoReplacingOutputStream(String filename, boolean replace) throws FileNotFoundException {
		this.filename = filename;
		this.replace = replace;
		if (!replace) {
            this.filename = getNonExistingFilename(filename);
		}
		this.fos = new FileOutputStream(this.filename);
	}
    
    /**
     * Gets the filename used to save the current state.
     * @return The filename used to save the current state.
     */
    public String getFilename()
    {
        return filename;
    }
    
    /**
     * Finds a non existing filename adding the suffix "_x" to the filename, 
     * where "x" is the number of version.
     * @param filename Destination filename.
     * @return A non existing filename.
     */
    private static String getNonExistingFilename(String filename) 
    {
        String name = new String(filename);
        File f = new File(name);
        
        while (f.exists()) {
            version++;
            name = getNewVersion(filename,version);
            f = null;
            f = new File(name);
        }
        return name;
    }
	
    /**
     * Gets the new name for the file using the current <b>newVersion</b>.
     * @param baseName Original filename.
     * @param newVersion Number of version to use.
     * @return The new filename to use.
     */
    private static String getNewVersion(String baseName, int newVersion)
    {
        //there are extension part
        int index = baseName.lastIndexOf('.');
        if (index != -1)
        {
            return  baseName.substring(0,index)+
                    "_" + newVersion +
                    baseName.substring(index,baseName.length());
        }
        return baseName + "_" + newVersion;
    }
    
	/**
	 * @see java.io.OutputStream#write(int)
	 */
	public void write(int b) throws IOException {
		fos.write(b);
	}
	
	/**
	 * @see java.io.OutputStream#close()
	 */
	public void close() throws IOException {
		fos.close();
	}
	
	/**
	 * @see java.io.OutputStream#flush()
	 */
	public void flush() throws IOException {
		fos.flush();
	}
	
	/**
	 * @see java.io.OutputStream#write(byte[], int, int)
	 */
	public void write(byte[] b, int off, int len) throws IOException {
		fos.write(b, off, len);
	}
	
	/**
	 * @see java.io.OutputStream#write(byte[])
	 */
	public void write(byte[] b) throws IOException {
		fos.write(b);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -