All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.thevpc.nuts.toolbox.nsh.bundles.jshell.AbstractJShellHistory Maven / Gradle / Ivy

There is a newer version: 0.8.3.1
Show newest version
package net.thevpc.nuts.toolbox.nsh.bundles.jshell;

import java.io.*;

public abstract class AbstractJShellHistory implements JShellHistory {
    @Override
    public void load(File file) throws IOException {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            String line=null;
            while((line=bufferedReader.readLine())!=null){
                add(line);
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }
    }

    @Override
    public void load(Reader reader) throws IOException {
        BufferedReader bufferedReader = null;
        try {
            if(reader instanceof BufferedReader) {
                bufferedReader = (BufferedReader) reader;
            }else{
                bufferedReader = new BufferedReader(reader);
            }
            String line=null;
            while((line=bufferedReader.readLine())!=null){
                add(line);
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }
    }

    @Override
    public void save(PrintWriter writer) throws IOException {
        for (String element : getElements()) {
            writer.println(element);
        }
    }

    @Override
    public void save(PrintStream writer) throws IOException {
        for (String element : getElements()) {
            writer.println(element);
        }
    }

    @Override
    public void save(File file) throws IOException {
        if(file==null){
            return;
        }
        if(file.getParentFile()!=null){
            file.getParentFile().mkdirs();
        }
        PrintWriter w=null;
        try{
            w=new PrintWriter(new FileWriter(file));
            save(w);
        }finally {
            if(w!=null){
                w.close();
            }
        }
    }

    @Override
    public void append(JShellHistory other) {
        for (String element : other.getElements(-1)) {
            add(element);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy