samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/usage/QueryStream.java
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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 // BEGIN: trycatchredo.stream
10 public final class QueryStream extends OutputStream {
11 private ByteArrayOutputStream arr = new ByteArrayOutputStream();
12 /** this field can be manipulated by the QueryException */
16 public synchronized void write(byte[] b, int off, int len)
18 if (reverse == null) {
19 throw new QueryException();
21 arr.write(b, off, len);
25 public synchronized void write(int b) throws IOException {
26 if (reverse == null) {
27 throw new QueryException();
33 public String toString() {
34 if (reverse == null) {
35 return "Reverse question was not answered yet!";
38 StringBuilder sb = new StringBuilder();
39 sb.append(arr.toString());
43 return arr.toString();
46 private class QueryException extends UserQuestionException {
49 public JOptionPane getQuestionPane() {
50 JOptionPane p = new JOptionPane("Store in reverse way?");
51 p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
56 public void confirm(Object option) {
57 if (option.equals(JOptionPane.YES_OPTION)) {
58 reverse = Boolean.TRUE;
61 if (option.equals(JOptionPane.NO_OPTION)) {
62 reverse = Boolean.FALSE;
68 // END: trycatchredo.stream