Package com.ontology2.centipede.shell

Source Code of com.ontology2.centipede.shell.ResourceAwareObject

package com.ontology2.centipede.shell;

import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;
import com.google.common.io.InputSupplier;
import com.ontology2.centipede.shell.CommandLineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import static com.google.common.base.Charsets.*;

abstract public class ResourceAwareObject {
    @Autowired
    ResourceLoader loader;

    // Wouldn't it be nice if we wrote some tests?

    protected ByteSource stream(final String resourceId) {
        return new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return loader.getResource(resourceId).getInputStream();
            }
        };
    }

    protected CharSource reader(final String resourceId) {
        return new CharSource() {
            @Override
            public Reader openStream() throws IOException {
                return new InputStreamReader(loader.getResource(resourceId).getInputStream(), UTF_8);
            }
        };
    }

    protected String string(final String resourceId) throws IOException {
        return reader(resourceId).read();
    }
}
TOP

Related Classes of com.ontology2.centipede.shell.ResourceAwareObject

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.