Proper way to use Generics in this example? - java

FileReader fileReader = null;
Object reader = null;
String dataRow = null;
fileReader = new FileReader(new File(fileLocation));
if (extension.equals("csv"))
{
reader = new CSVReader(fileReader);
}
else
{
reader = new BufferedReader(fileReader);
}
while (null != (dataRow = reader.readLine()))
{
...
}
The idea is to use different types depending on the file type in order to remove duplicated code. However, I get an error on the last line, since reader is type Object. Thanks for the help.

Maybe you could make 2 methods:
public String read(CSVReader c){
return c.readLine();
}
public String read(BufferedReader br){
return br.readLine();
}
Then, in your current code:
if(extension.equals("csv"))
dataRow = read(new CSVReader(fileReader));
else
dataRow = read(new BufferedReader(fileReader));
This overloading would remove the need for a wrapper class.
If you really want to use a wrapper class, I recommend having this somewhere:
public interface MyIO{
public String readLine();
}
public class MyBr extends BufferedReader implements MyIO{}
public class MyCSV extends CSVReader implements MyIO{}
Then, in your code:
MyIO reader;
if(extension.equals("csv"))
reader = new MyCSV(fileReader);
else
reader = new MyBr(fileReader);
You'd notice that both are the same number of lines of code and (in my opinion) the methods are easier to follow.

Just answering to point out that it is certainly possible to use generics even if your types are not cooperative. You'll just have to define specializations for each type separately. I'll just put a sketch in Java 8 here. Not sure what you mean by 'Proper way', there are pros and cons to everything...especially in Java.
Somewhat simpler way, putting generic code in a common superclass:
interface GenericExample {
interface InputGenericCode<Input> {
/**
* This is implemented in subtypes.
*
* #param x
* #return
*/
String readLine(Input x);
default void genericAlgorithm(Input x) {
// algorithm expressed generically here...
for (;;) {
String lineString = readLine(x);
System.out.println("" + lineString);
}
}
}
public class BufferedReaderInputGenericCode implements InputGenericCode<BufferedReader> {
#Override
public String readLine(BufferedReader x) {
try {
return x.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class CSVReaderInputGenericCode implements InputGenericCode<CSVReader> {
#Override
public String readLine(CSVReader x) {
return x.readLine();
}
}
static class CSVReader {
public CSVReader(FileReader fileReader) {
throw new RuntimeException("implement this");
}
public String readLine() {
throw new RuntimeException("implement this");
}
}
public static void main(String fileLocation, String extension) {
FileReader fileReader = openFile(fileLocation);
if (extension.equals("csv")) {
CSVReader reader = new CSVReader(fileReader);
new CSVReaderInputGenericCode().genericAlgorithm(reader);
} else {
BufferedReader reader = new BufferedReader(fileReader);
new BufferedReaderInputGenericCode().genericAlgorithm(reader);
}
// dataRow = reader.readLine();
}
public static FileReader openFile(String fileLocation) {
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(fileLocation));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return fileReader;
}
}
More complex way:
interface GenericExample {
/**
* All generic operations.
*
* #author jonasn
*
* #param <Input>
*/
interface InputGenerics<Input> {
String readLine(Input x);
}
interface InputGenericCode {
public static <Input> void genericAlgorithm(Input x, InputGenerics<Input> generics) {
// algorithm expressed generically here...
for (;;) {
String lineString = generics.readLine(x);
System.out.println("" + lineString);
}
}
}
static class CSVReader {
public CSVReader(FileReader fileReader) {
// TODO Auto-generated constructor stub
}
public String readLine() {
throw new RuntimeException("not implemented");
}
}
public class CSVReaderInputGenerics implements InputGenerics<CSVReader> {
#Override
public String readLine(CSVReader x) {
return x.readLine();
}
}
public class BufferedReaderInputGenerics implements InputGenerics<BufferedReader> {
#Override
public String readLine(BufferedReader x) {
try {
return x.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
public static void main(String fileLocation, String extension) {
// String fileLocation = "whatever";
// String extension = "";
FileReader fileReader = null;
// Object reader = null;
String dataRow = null;
try {
fileReader = new FileReader(new File(fileLocation));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
if (extension.equals("csv"))
{
CSVReader reader = new CSVReader(fileReader);
InputGenericCode.genericAlgorithm(reader, new CSVReaderInputGenerics());
}
else
{
BufferedReader reader = new BufferedReader(fileReader);
InputGenericCode.genericAlgorithm(reader, new BufferedReaderInputGenerics());
}
// dataRow = reader.readLine();
}
}

Related

How to parse mulitple XML files in Java with XMLStreamReader

We have to parse xml info from a generator that creates fake weather data for a bunch of weatherstations. Currently we're just printing it, but we'll have to do stuff with it later.
However, the data we receive consists of multiple XML "files". Is there a way to separate the data and split it at a new <?xml...?>? (The data is a continuous stream that randomly splits)
our code:
public class Main {
static private final int portNumber = Null;
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
BufferedReader clientReader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(clientReader);
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
try {
String text = reader.getElementText();
System.out.println("Element Local Name:" + reader.getLocalName());
System.out.println("Text:" + text);
} catch (XMLStreamException e) {
System.out.println(e);
}
}
else if(event == XMLStreamConstants.END_ELEMENT){
reader.close();
}
}
} catch (IOException e) {
System.out.println("Error: Unable to Start Server Socket\n\t" + e);
} catch (XMLStreamException e){
System.out.println(e);
}
}
}
example of the xml (of which we receive multiple after each other):
<?xml version="1.0"?>
<!-- The WEATHERDATA-element contains multiple MEASUREMENT-elements -->
<WEATHERDATA>
<MEASUREMENT>
<STN>123456</STN>
<DATE>2009-09-13</DATE>
<TIME>15:59:46</TIME>
<TEMP>-60.1</TEMP>
<DEWP>-58.1</DEWP>
<STP>1034.5</STP>
<SLP>1007.6</SLP>
<VISIB>123.7</VISIB>
<WDSP>10.8</WDSP>
<PRCP>11.28</PRCP>
<SNDP>11.1</SNDP>
<FRSHTT>010101</FRSHTT>
<CLDC>87.4</CLDC>
<WNDDIR>342</WNDDIR>
</MEASUREMENT>
</WEATHERDATA>
We also have a dtd file but I'm not sure if that's helpful.
Using java.util.Scanner may serve as a quick workaround. The disassemble() function skips the XML declaration if present and combines all characters up to and including the next closing </WEATHERDATA> tag into a `String'. The result is then passed to the callback which in this example converts XML into a POJO with JAXB.
What I don't like about Scanner is that it internally buffers the input stream so it is possible to lose the last message when the stream is closed.
public class DisassembleXml {
private static final int port = 8888;
private static final Pattern XML_DECL_PATTERN = Pattern.compile("<\\?xml.*?\\?>");
private static final Pattern DATA_PATTERN =
Pattern.compile(".*?</WEATHERDATA>\\s+", Pattern.DOTALL);
public static void main(String[] args) throws Exception {
final ServerSocket serverSocket = new ServerSocket(port);
System.out.printf("Listening on %d%n", serverSocket.getLocalPort());
final Socket clientSocket = serverSocket.accept();
System.out.printf("Processing from %s%n", clientSocket);
try (Reader sr = new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.ISO_8859_1))
{
disassemble(sr, new ConvertToPojoAndPrint());
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void disassemble(Reader reader, Consumer<String> xmlConsumer) {
final Scanner sc = new Scanner(reader).useDelimiter("\\Z");
try {
while (true) {
final String xml = sc
.skip(XML_DECL_PATTERN)
.findWithinHorizon(DATA_PATTERN, 0);
if (xml == null || xml.isEmpty())
break;
xmlConsumer.accept(xml);
}
}
catch (Exception e) {
throw new IllegalStateException("cannot interpret stream", e);
}
}
private static class ConvertToPojoAndPrint implements Consumer<String>
{
final JAXBContext jaxbContext;
final Unmarshaller unmarshaller;
ConvertToPojoAndPrint() throws JAXBException {
jaxbContext = JAXBContext.newInstance(WeatherData.class);
unmarshaller = jaxbContext.createUnmarshaller();
}
#Override
public void accept(String xml) {
try {
final WeatherData weatherData = (WeatherData) unmarshaller.unmarshal(new StringReader(xml));
System.out.println("Another sample: " + weatherData);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
#XmlRootElement(name = "WEATHERDATA")
private static class WeatherData
{
#XmlElement(name = "MEASUREMENT")
Measurement measurement;
#Override
public String toString() { return "WeatherData{" + "measurement=" + measurement + '}'; }
}
private static class Measurement
{
#XmlElement(name = "STN")
String stn;
// ... skipping the rest of elements for brevity
#Override
public String toString() { return "Measurement{" + "stn='" + stn + '\'' + '}'; }
}
}

Declaring an array in java as an object?

I have recently been following some tutorials on how to program and whilst programming the public static void for an array, the tutorial said to declare the array as an object. Below is the code for the array and at the end of the code I have put a split between the two sections so it is visible to as where my question lies
import java.io.*;
import java.lang.*;
public class LoginList
{
int arraySize=500;
Login[] arrayLogin=new Login[arraySize];
int nextPosition=0;
int LoginLocation=-1;
public void addLogin(Login tempLoginParameters)
{
arrayLogin[nextPosition] = tempLoginParameters;
nextPosition++;
}
public void writeLogins()
{
try
{
BufferedWriter LoginWriter = new BufferedWriter(new FileWriter("LoginDetails.txt"));
for(int i=0;i<nextPosition;i++)
{
LoginWriter.write(arrayLogin[i].toString());
LoginWriter.newLine();
}
LoginWriter.close();
}
catch(Exception e)
{
System.out.println("Error with writer");
}
}
public void readLogins()
{
try
{
BufferedReader LoginReader = new BufferedReader(new FileReader("LoginDetails.txt"));
String ReadLine = LoginReader.readLine();
while(ReadLine!= null)
{
String[] arrayStringLogin = ReadLine.split(", ");
Login tempLogin = new Login();
tempLogin.UserName = arrayStringLogin[0];
tempLogin.Password = arrayStringLogin[1];
arrayLogin[nextPosition] = tempLogin;
nextPosition++;
ReadLine = LoginReader.readLine();
}
}
catch(Exception e)
{
System.out.println("Error with reader");
}
}
public void displayLoginDetails()
{
for(int i=0;i<nextPosition;i++)
{
System.out.println("Login "+nextPosition+": "+arrayLogin[i].toString());
}
}
public void searchLogins(String TempLog)
{
LoginLocation=-1;
for(int i=0;i<nextPosition;i++)
{
if(arrayLogin[i].UserName.equals(TempLog))
{
System.out.println("Match At Position:"+i);
LoginLocation=i;
}
else
{
System.out.println("No match for UserName");
}
}
}
public static void main(String[] args)
{
LoginList ll = new LoginList(); //Declares the array as an object
Why is it that you have to declare the array as an object? Look just above here.
Login tempLogin = new Login();
ll.readLogins();
ll.displayLoginDetails();
}
}
LoginList is not an array, it's a class that happens to have an array of Login objects as one of its instance members. The code in main creates an object of type LoginList and calls its methods; the LoginList object uses an array internally, but the main method doesn't have to know about it.

Java run linux(raspbian) command(omxplayer) and get output

I create a program as below to execute a linux (raspbian) command: "omxplayer".
But I don't know why I cannot get output from omxplayer as the time I type it into command line and hit Enter.But the output only show at the end of the video.
So I want to get the output immediately after I type "omxplayer [video_name]" and hit "Enter" in my program.
Just like the command line (terminal) work when I type directly into it in linux.
This is my code:
public class testprog {
public static void main(String args[]) throws IOException {
String in = "";
while(in!="exit")
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
in = reader.readLine();
runCommand(in);
}
}
public static void runCommand(String command)
{
String s;
Process p;
try {
System.out.println("run command " + command);
p = Runtime.getRuntime().exec(new String[]{"bash", "-c",command});
MyInputStreamReader reader1 = new MyInputStreamReader(p.getInputStream());
reader1.setTag("in");
reader1.start();
MyInputStreamReader reader2 = new MyInputStreamReader(p.getErrorStream());
reader2.setTag("in");
reader2.start();
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
}
}
class MyInputStreamReader extends Thread{
boolean isStop = false;
ReadEventHandler handler;
String tag;
InputStream in;
public MyInputStreamReader(InputStream in)
{
this.in = in;
}
public void setHandler(ReadEventHandler handler) {
this.handler = handler;
}
public void setTag(String tag)
{
this.tag = tag;
}
public void run()
{
byte[] buff = new byte[8192];
while (true) {
//String line;
try {
int len = in.read(buff);
if (len == -1)
{
return;
}
String line = new String(buff, 0, len);
if (handler!=null)
handler.onReceived(line);
System.out.println(tag +" " + line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void dispose()
{
this.isStop = true;
}
public interface ReadEventHandler
{
void onReceived(String line);
}
}
Any response is highly appreciated. Thanks
Did you checked this?
http://javedmandary.blogspot.com/2014/01/firing-up-raspberry-pi-omxplayer-using.html
I guess there is the code you're looking for.

Java reading and writing to same file

I'm using the following code to search specific files in my computer and write the absolute path in a text file. My problem is that every time I run this code it add duplicate lines into text file, i want to add only those lines(file path) which are not written in the text file at that time (no duplicates).. Thank you
public static void walkin(File dir) {
String pattern = ".mp4";
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("D:\\nawaaaaaa.txt", true)));
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkin(listFile[i]);
} else if (listFile[i].getName().endsWith(pattern)
&& listFile[i].isFile()) {
System.out.println(listFile[i].getPath());
out.write(listFile[i].toString());
out.write("\r\n");
// out.close();
} else {
walkin(listFile[i]);
}
}
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Your code works for me, no idea what is the problem on your side, how you are calling it; but you can optimize your code a bit, something as follows (just very quick code, code be made nicer, but to give you an idea):
public class SomeTest {
private static HashSet<String> filez = new HashSet<String> ();
public static void walkin(File dir, PrintWriter out) {
String pattern = ".mp4";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].getName().endsWith(pattern) && listFile[i].isFile()) {
//System.out.println(listFile[i].getPath());
if (filez.add(listFile[i].getPath())) {
out.write(listFile[i].toString());
out.write("\r\n");
}
} else {
walkin(listFile[i], out);
}
}
}
}
public static void main(String[] args) {
try {
File dir = new File("C:\\mydir");
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("D:\\nawaaaaaa.txt", true)));
walkin(dir, out);
out.close();
} catch (IOException e) {
//
}
}
}
You can use the filez hashset to print stuff, or write your file at the end of the parsing process as well.. your choice.
If you don't want duplicates in the file, you will need to keep track of the file names you have already written. A HashSet<String> is going for this. But I'm surprised the above code works at all given that you keep opening the file at the top of walkin() and walkin() itself is recursive. You need to rethink your code a bit. Possibly passing the PrintWriter into walkin() as a parameter.
Since you are running the code multiple times ("every time I run this code it add duplicate lines into text file"), so once you finish writing to the file, you read each line and store it in a HashSet<String>. And use another writer to write it to the file.
BufferedWriter writer = new BufferedWriter(new FileWriter("filename"));
for (String eachUniqueLine: `Your hash set`) {
writer.write(eachUniqueLine);
writer.newLine();
}
(It is costly as in you have to do more i/o operation)
You need to expand your method into a class that perform this kind of tasks.
You have two main problem you open a writer for each directory and you call the walkin, for things that do not apply to your logic (and open writer again).
You should try to design a class that will be able to create an index for you.
public static void main(String[] args) throws IOException {
File createTempFile = File.createTempFile("mp4", ".idx");
FileIndexer fi = new FileIndexer(createTempFile.getAbsolutePath());
fi.index("C:\\", "mp4");
System.out.println(createTempFile);
}
public static class FileIndexer {
private static final String END_OF_LINE = "\r\n";
private final String outputPath;
private final Set<String> index = new HashSet<String>();
public FileIndexer(String outputPath) {
this.outputPath = outputPath;
}
private boolean isValidPath(String path) {
return outputPath != null && outputPath.trim().length() > 0;
}
private boolean isValidIndexFile(File file) {
return file.isFile() && file.canRead() && file.canWrite();
}
private void createIndexFile(File file) throws IOException {
if(file.createNewFile() == false) {
throw new IOException("Could not create index file");
}
this.index.clear();
}
private void readIndexFile(File file) throws IOException {
isValidIndexFile(file);
index.clear();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
String line;
while((line = bufferedReader.readLine()) != null) {
addToIndex(line);
}
} finally {
if(bufferedReader != null) {
bufferedReader.close();
}
}
}
private void addToIndex(String line) {
index.add(line);
}
private PrintWriter openIndex() throws IOException {
if(isValidPath(outputPath) == false) {
throw new IOException(String.format("The outputPath is not valid: [%s]",outputPath));
}
File indexFile = new File(outputPath);
if(indexFile.exists()) {
readIndexFile(indexFile);
} else {
createIndexFile(indexFile);
}
return new PrintWriter(new BufferedWriter(new FileWriter(this.outputPath, true)));
}
public synchronized void index(String pathToIndex, String pattern) throws IOException {
isValidPath(pathToIndex);
PrintWriter out = openIndex();
try {
File elementToIndex = new File(pathToIndex);
index(elementToIndex,pathToIndex, out);
} finally {
if(out != null) {
out.close();
}
}
}
private void index(File elementToIndex, String pattern, PrintWriter out) {
if(elementToIndex == null) {
return;
}
if(elementToIndex.isDirectory()) {
for(File file : elementToIndex.listFiles()) {
index(file,pattern, out);
}
}
if(elementToIndex.isFile() && elementToIndex.getAbsolutePath().endsWith(pattern)) {
writeToIndex(elementToIndex, out);
}
}
private void writeToIndex(File elementToIndex, PrintWriter out) {
out.write(elementToIndex.getAbsolutePath());
out.write(END_OF_LINE);
}
}
Problem Solved (BTW i'm not sure if it is most efficient solution or not ).......
public static void main(String[] args) {
try {
File dir = new File("D:\\To Do");
BufferedWriter out = new BufferedWriter(new FileWriter(
"D:\\path.txt", true));
walkin(dir, out);
out.close();
readfile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Replace this with a suitable directory
// walkin(new File("D:/to Do"));
}
public static void walkin(File dir, BufferedWriter out) throws IOException {
String pattern = ".mp4";
// BufferedWriter out = new BufferedWriter(
// new FileWriter("D:\\path.txt",true));
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].getName().endsWith(pattern)
&& listFile[i].isFile()) {
if (filez.add(listFile[i].getPath())) {
// System.out.println(listFile[i].getPath());
out.write(listFile[i].toString());
out.write("\r\n");
// System.out.println(filez);
}
} else {
walkin(listFile[i], out);
}
}
}
}
public static void readfile() {
BufferedReader br = null;
String str;
try {
BufferedWriter out = new BufferedWriter(new FileWriter(
"D:\\duplicate_free.txt"));
br = new BufferedReader(new FileReader("D:\\path.txt"));
while ((str = br.readLine()) != null) {
if (files.contains(str)) {
} else {
files.add(str);
}
}
for (String uniq : files) {
out.write(uniq);
System.out.println(uniq);
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

What is the best way to write to a file in a parallel thread in Java?

I have a program that performs lots of calculations and reports them to a file frequently. I know that frequent write operations can slow a program down a lot, so to avoid it I'd like to have a second thread dedicated to the writing operations.
Right now I'm doing it with this class I wrote (the impatient can skip to the end of the question):
public class ParallelWriter implements Runnable {
private File file;
private BlockingQueue<Item> q;
private int indentation;
public ParallelWriter( File f ){
file = f;
q = new LinkedBlockingQueue<Item>();
indentation = 0;
}
public ParallelWriter append( CharSequence str ){
try {
CharSeqItem item = new CharSeqItem();
item.content = str;
item.type = ItemType.CHARSEQ;
q.put(item);
return this;
} catch (InterruptedException ex) {
throw new RuntimeException( ex );
}
}
public ParallelWriter newLine(){
try {
Item item = new Item();
item.type = ItemType.NEWLINE;
q.put(item);
return this;
} catch (InterruptedException ex) {
throw new RuntimeException( ex );
}
}
public void setIndent(int indentation) {
try{
IndentCommand item = new IndentCommand();
item.type = ItemType.INDENT;
item.indent = indentation;
q.put(item);
} catch (InterruptedException ex) {
throw new RuntimeException( ex );
}
}
public void end(){
try {
Item item = new Item();
item.type = ItemType.POISON;
q.put(item);
} catch (InterruptedException ex) {
throw new RuntimeException( ex );
}
}
public void run() {
BufferedWriter out = null;
Item item = null;
try{
out = new BufferedWriter( new FileWriter( file ) );
while( (item = q.take()).type != ItemType.POISON ){
switch( item.type ){
case NEWLINE:
out.newLine();
for( int i = 0; i < indentation; i++ )
out.append(" ");
break;
case INDENT:
indentation = ((IndentCommand)item).indent;
break;
case CHARSEQ:
out.append( ((CharSeqItem)item).content );
}
}
} catch (InterruptedException ex){
throw new RuntimeException( ex );
} catch (IOException ex) {
throw new RuntimeException( ex );
} finally {
if( out != null ) try {
out.close();
} catch (IOException ex) {
throw new RuntimeException( ex );
}
}
}
private enum ItemType {
CHARSEQ, NEWLINE, INDENT, POISON;
}
private static class Item {
ItemType type;
}
private static class CharSeqItem extends Item {
CharSequence content;
}
private static class IndentCommand extends Item {
int indent;
}
}
And then I use it by doing:
ParallelWriter w = new ParallelWriter( myFile );
new Thread(w).start();
/// Lots of
w.append(" things ").newLine();
w.setIndent(2);
w.newLine().append(" more things ");
/// and finally
w.end();
While this works perfectly well, I'm wondering:
Is there a better way to accomplish this?
Your basic approach looks fine. I would structure the code as follows:
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public interface FileWriter {
FileWriter append(CharSequence seq);
FileWriter indent(int indent);
void close();
}
class AsyncFileWriter implements FileWriter, Runnable {
private final File file;
private final Writer out;
private final BlockingQueue<Item> queue = new LinkedBlockingQueue<Item>();
private volatile boolean started = false;
private volatile boolean stopped = false;
public AsyncFileWriter(File file) throws IOException {
this.file = file;
this.out = new BufferedWriter(new java.io.FileWriter(file));
}
public FileWriter append(CharSequence seq) {
if (!started) {
throw new IllegalStateException("open() call expected before append()");
}
try {
queue.put(new CharSeqItem(seq));
} catch (InterruptedException ignored) {
}
return this;
}
public FileWriter indent(int indent) {
if (!started) {
throw new IllegalStateException("open() call expected before append()");
}
try {
queue.put(new IndentItem(indent));
} catch (InterruptedException ignored) {
}
return this;
}
public void open() {
this.started = true;
new Thread(this).start();
}
public void run() {
while (!stopped) {
try {
Item item = queue.poll(100, TimeUnit.MICROSECONDS);
if (item != null) {
try {
item.write(out);
} catch (IOException logme) {
}
}
} catch (InterruptedException e) {
}
}
try {
out.close();
} catch (IOException ignore) {
}
}
public void close() {
this.stopped = true;
}
private static interface Item {
void write(Writer out) throws IOException;
}
private static class CharSeqItem implements Item {
private final CharSequence sequence;
public CharSeqItem(CharSequence sequence) {
this.sequence = sequence;
}
public void write(Writer out) throws IOException {
out.append(sequence);
}
}
private static class IndentItem implements Item {
private final int indent;
public IndentItem(int indent) {
this.indent = indent;
}
public void write(Writer out) throws IOException {
for (int i = 0; i < indent; i++) {
out.append(" ");
}
}
}
}
If you do not want to write in a separate thread (maybe in a test?), you can have an implementation of FileWriter which calls append on the Writer in the caller thread.
One good way to exchange data with a single consumer thread is to use an Exchanger.
You could use a StringBuilder or ByteBuffer as the buffer to exchange with the background thread. The latency incurred can be around 1 micro-second, doesn't involve creating any objects and which is lower using a BlockingQueue.
From the example which I think is worth repeating here.
class FillAndEmpty {
Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
DataBuffer initialEmptyBuffer = ... a made-up type
DataBuffer initialFullBuffer = ...
class FillingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer != null) {
addToBuffer(currentBuffer);
if (currentBuffer.isFull())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { ... handle ... }
}
}
class EmptyingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialFullBuffer;
try {
while (currentBuffer != null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.isEmpty())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { ... handle ...}
}
}
void start() {
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
}
Using a LinkedBlockingQueue is a pretty good idea. Not sure I like some of the style of the code... but the principle seems sound.
I would maybe add a capacity to the LinkedBlockingQueue equal to a certain % of your total memory.. say 10,000 items.. this way if your writing is going too slow, your worker threads won't keep adding more work until the heap is blown.
I know that frequent write operations
can slow a program down a lot
Probably not as much as you think, provided you use buffering.

Categories