gate.io.JsonElementReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gate Show documentation
Show all versions of gate Show documentation
A multipurpose java library
package gate.io;
import gate.error.ConversionException;
import gate.lang.json.JsonElement;
import gate.lang.json.JsonParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class JsonElementReader extends AbstractReader>
{
private static final JsonElementReader INSTANCE = new JsonElementReader();
private static final ConcurrentMap INSTANCES = new ConcurrentHashMap<>();
private JsonElementReader()
{
}
private JsonElementReader(String charset)
{
super(charset);
}
@Override
public Optional read(InputStream is) throws IOException
{
try
{
return new JsonParser(new BufferedReader(new InputStreamReader(is)))
.stream().findAny();
} catch (ConversionException ex)
{
throw new IOException(ex.getMessage(), ex);
}
}
public static JsonElementReader getInstance()
{
return INSTANCE;
}
public static JsonElementReader getInstance(String charset)
{
return INSTANCES.computeIfAbsent(charset, JsonElementReader::new);
}
public static Optional read(File file) throws IOException
{
try (FileInputStream is = new FileInputStream(file))
{
return JsonElementReader.getInstance().read(is);
}
}
public static Optional read(String charset, File file) throws IOException
{
try (FileInputStream is = new FileInputStream(file))
{
return JsonElementReader.getInstance(charset).read(is);
}
}
public static Optional read(URL url) throws IOException
{
try (InputStream is = url.openStream())
{
return JsonElementReader.getInstance().read(is);
}
}
public static Optional read(String charset, URL url) throws IOException
{
try (InputStream is = url.openStream())
{
return JsonElementReader.getInstance(charset).read(is);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy