samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/usage/QueryStream.java
1 package org.apidesign.exceptions.trycatchredo.usage;
3 import org.apidesign.exceptions.trycatchredo.api.UserQuestionException;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import javax.swing.JOptionPane;
9 public final class QueryStream extends OutputStream {
10 private ByteArrayOutputStream arr = new ByteArrayOutputStream();
11 /** this field can be manipulated by the QueryException */
15 public synchronized void write(byte[] b, int off, int len) throws IOException {
16 if (reverse == null) {
17 throw new QueryException();
19 arr.write(b, off, len);
23 public synchronized void write(int b) throws IOException {
24 if (reverse == null) {
25 throw new QueryException();
31 public String toString() {
32 if (reverse == null) {
33 return "Reverse question was not answered yet!";
36 StringBuilder sb = new StringBuilder();
37 sb.append(arr.toString());
41 return arr.toString();
44 private class QueryException extends UserQuestionException {
47 public JOptionPane getQuestionPane() {
48 JOptionPane p = new JOptionPane("Store in reverse way?");
49 p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
54 public void confirm(Object option) {
55 if (option.equals(JOptionPane.YES_OPTION)) {
56 reverse = Boolean.TRUE;
59 if (option.equals(JOptionPane.NO_OPTION)) {
60 reverse = Boolean.FALSE;