Gridarta Editor
CopyReader.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.io.CharArrayWriter;
23 import java.io.IOException;
24 import java.io.Reader;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
32 public class CopyReader {
33 
37  @NotNull
38  private final Reader reader;
39 
44  @NotNull
45  private final Object sync = new Object();
46 
50  @NotNull
51  private final CharArrayWriter charArrayWriter = new CharArrayWriter();
52 
56  private boolean stopped;
57 
61  @Nullable
62  private String failure;
63 
67  @NotNull
68  private final Runnable runnable = new Runnable() {
69 
70  @Override
71  public void run() {
72  final char[] buf = new char[1024];
73  while (!Thread.currentThread().isInterrupted()) {
74  try {
75  final int len = reader.read(buf);
76  if (len == -1) {
77  break;
78  }
79  synchronized (sync) {
80  if (stopped) {
81  break;
82  }
83  charArrayWriter.write(buf, 0, len);
84  }
85  } catch (final IOException ex) {
86  setFailure(ex.getMessage());
87  }
88  }
89  }
90 
91  };
92 
96  @NotNull
97  private final Thread thread = new Thread(runnable);
98 
103  public CopyReader(@NotNull final Reader reader) {
104  this.reader = reader;
105  }
106 
110  public void start() {
111  thread.start();
112  }
113 
117  public void stop() {
118  thread.interrupt();
119  }
120 
126  @SuppressWarnings("NullableProblems")
127  private void setFailure(@NotNull final String failure) {
128  synchronized (sync) {
129  stopped = true;
130  if (this.failure == null) {
131  this.failure = failure;
132  }
133  }
134  }
135 
140  @Nullable
141  public String getFailure() {
142  synchronized (sync) {
143  stopped = true;
144  thread.interrupt();
145  return failure;
146  }
147  }
148 
153  @NotNull
154  public String getOutput() {
155  synchronized (sync) {
156  stopped = true;
157  thread.interrupt();
158  return charArrayWriter.toString();
159  }
160  }
161 
167  public void join() throws InterruptedException {
168  thread.join();
169  }
170 
171 }
boolean stopped
Whether the worker thread was stopped.
Definition: CopyReader.java:56
String failure
The failure reason.
Definition: CopyReader.java:62
final Object sync
The synchronization object for accessing stopped, failure, and charArrayWriter.
Definition: CopyReader.java:45
final CharArrayWriter charArrayWriter
The CharArrayWriter collecting data read from reader.
Definition: CopyReader.java:51
void start()
Starts reading.
Copies a Reader into a String.
Definition: CopyReader.java:32
final Runnable runnable
The Runnable implementing the worker thread.
Definition: CopyReader.java:68
final Thread thread
The worker Thread executing runnable.
Definition: CopyReader.java:97
String getOutput()
Returns the reader's output.
final Reader reader
The Reader to read from.
Definition: CopyReader.java:38
void stop()
Stops reading.
void setFailure(@NotNull final String failure)
Sets the failure reason.
String getFailure()
Returns the failure reason.
void join()
Waits for the worker thread to terminate.
CopyReader(@NotNull final Reader reader)
Creates a new instance.