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-2023 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  private void setFailure(@NotNull final String failure) {
127  synchronized (sync) {
128  stopped = true;
129  if (this.failure == null) {
130  this.failure = failure;
131  }
132  }
133  }
134 
139  @Nullable
140  public String getFailure() {
141  synchronized (sync) {
142  stopped = true;
143  thread.interrupt();
144  return failure;
145  }
146  }
147 
152  @NotNull
153  public String getOutput() {
154  synchronized (sync) {
155  stopped = true;
156  thread.interrupt();
157  return charArrayWriter.toString();
158  }
159  }
160 
166  public void join() throws InterruptedException {
167  thread.join();
168  }
169 
170 }
net.sf.gridarta.utils.CopyReader.stopped
boolean stopped
Whether the worker thread was stopped.
Definition: CopyReader.java:56
net.sf.gridarta.utils.CopyReader.reader
final Reader reader
The Reader to read from.
Definition: CopyReader.java:38
net.sf.gridarta.utils.CopyReader.sync
final Object sync
The synchronization object for accessing stopped, {}, and #charArrayWriter}.
Definition: CopyReader.java:45
net.sf.gridarta.utils.CopyReader.start
void start()
Starts reading.
Definition: CopyReader.java:110
net.sf.gridarta.utils.CopyReader.getOutput
String getOutput()
Returns the reader's output.
Definition: CopyReader.java:153
net.sf.gridarta.utils.CopyReader.CopyReader
CopyReader(@NotNull final Reader reader)
Creates a new instance.
Definition: CopyReader.java:103
net.sf.gridarta.utils.CopyReader.join
void join()
Waits for the worker thread to terminate.
Definition: CopyReader.java:166
net.sf.gridarta.utils.CopyReader.runnable
final Runnable runnable
The Runnable implementing the worker thread.
Definition: CopyReader.java:68
net.sf.gridarta.utils.CopyReader.charArrayWriter
final CharArrayWriter charArrayWriter
The CharArrayWriter collecting data read from reader.
Definition: CopyReader.java:51
net.sf.gridarta.utils.CopyReader.stop
void stop()
Stops reading.
Definition: CopyReader.java:117
net.sf.gridarta.utils.CopyReader.setFailure
void setFailure(@NotNull final String failure)
Sets the failure reason.
Definition: CopyReader.java:126
net.sf.gridarta.utils.CopyReader
Copies a Reader into a String.
Definition: CopyReader.java:32
net.sf.gridarta.utils.CopyReader.getFailure
String getFailure()
Returns the failure reason.
Definition: CopyReader.java:140
net.sf.gridarta.utils.CopyReader.failure
String failure
The failure reason.
Definition: CopyReader.java:62
net.sf.gridarta.utils.CopyReader.thread
final Thread thread
The worker Thread executing runnable.
Definition: CopyReader.java:97