
br.com.objectos.io.Resource Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.io;
import static br.com.objectos.core.lang.Preconditions.checkArgument;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/**
* @author [email protected] (Marcio Endo)
*/
public class Resource implements ByteSource, CharSource {
private final URL url;
private Resource(URL url) {
this.url = url;
}
public static Resource of(String resourceName) {
return of(Resource.class, resourceName);
}
public static Resource of(Class> contextClass, String resourceName) {
URL url = contextClass.getResource(resourceName);
checkArgument(url != null, "Resource %s not found.", resourceName);
return new Resource(url);
}
@Override
public final Stream lines() throws IOException {
BufferedReader reader = openBufferedReader(Charset.defaultCharset());
try {
return reader.lines().onClose(asUncheckedRunnable(reader));
} catch (Error | RuntimeException e) {
try {
reader.close();
} catch (IOException ex) {
try {
e.addSuppressed(ex);
} catch (Throwable ignore) {
}
}
throw e;
}
}
@Override
public InputStream openStream() throws IOException {
return url.openStream();
}
@Override
public final List readAllLines() throws IOException {
return readAllLines(Charset.defaultCharset());
}
@Override
public final List readAllLines(Charset charset) throws IOException {
try (BufferedReader reader = openBufferedReader(charset)) {
List result = new ArrayList<>();
String line = reader.readLine();
while (line != null) {
result.add(line);
line = reader.readLine();
}
return result;
}
}
@Override
public final String readToString() throws IOException {
CharBuffer buf = CharBuffer.allocate(4096);
StringBuilder str = new StringBuilder();
try (Reader reader = new InputStreamReader(openStream())) {
while (reader.read(buf) != -1) {
buf.flip();
str.append(buf);
buf.clear();
}
}
return str.toString();
}
private BufferedReader openBufferedReader(Charset charset) throws IOException {
InputStream in = openStream();
InputStreamReader reader = new InputStreamReader(in, charset);
return new BufferedReader(reader);
}
private Runnable asUncheckedRunnable(Closeable c) {
return () -> {
try {
c.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy