00001
package com.quadcap.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.ByteArrayInputStream;
00042
import java.io.ByteArrayOutputStream;
00043
import java.io.File;
00044
import java.io.FileInputStream;
00045
import java.io.FileNotFoundException;
00046
import java.io.FileOutputStream;
00047
import java.io.InputStream;
00048
import java.io.IOException;
00049
import java.io.OutputStream;
00050
00051
00052
00053
00054
00055
00056
00057
00058 public class SaveRestoreStream {
00059 ByteArrayOutputStream
bos = null;
00060 FileOutputStream
fos = null;
00061 File
file = null;
00062 int len = 0;
00063 int max = 4096;
00064
00065
00066
00067
00068 public SaveRestoreStream() {
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 public SaveRestoreStream(
int max) {
00078
this.max = max;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 public SaveRestoreStream(InputStream in)
throws IOException {
00088 OutputStream out =
getOutputStream();
00089
try {
00090
IO.copyStream(in, out);
00091 } finally {
00092 out.close();
00093 }
00094 }
00095
00096
00097
00098
00099
00100
00101 public OutputStream
getOutputStream() throws IOException {
00102
if (
file == null) {
00103
bos =
new ByteArrayOutputStream();
00104
if (
fos != null)
fos.close();
00105
fos = null;
00106 }
else {
00107
bos = null;
00108
fos =
new FileOutputStream(
file);
00109 }
00110
00111
return new OutputStream() {
00112
public void write(
int c)
throws IOException {
00113
doWrite(c);
00114 }
00115
public void flush()
throws IOException {
00116
if (
bos != null)
bos.flush();
00117
else fos.flush();
00118 }
00119
public void close()
throws IOException {
00120
if (
bos != null) {
00121
len =
bos.size();
00122 }
else if (
fos != null) {
00123
fos.close();
00124
fos = null;
00125
len = (
int)
file.length();
00126 }
00127 }
00128 };
00129 }
00130
00131 public int length() {
00132
return len;
00133 }
00134
00135 static int tmpFileCount = 0;
00136
00137 static synchronized File
tmpFile() throws IOException {
00138
return File.createTempFile(
"" + System.currentTimeMillis() +
"." +
00139 (
tmpFileCount++),
".tmp");
00140 }
00141
00142
00143
00144
00145
00146
00147
00148 public void doWrite(
int c)
throws IOException {
00149
if (
bos != null) {
00150
if (
bos.size() <
max) {
00151
bos.write(c);
00152 }
else {
00153
file =
tmpFile();
00154
fos =
new FileOutputStream(
file);
00155
bos.writeTo(
fos);
00156
fos.write(c);
00157
bos.close();
00158
bos = null;
00159 }
00160 }
else {
00161
fos.write(c);
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170 public InputStream
getInputStream() throws IOException {
00171
return getInputStream(
true);
00172 }
00173
00174 public InputStream
getInputStream(
final boolean del)
throws IOException {
00175
if (
bos != null)
return new ByteArrayInputStream(
bos.toByteArray());
00176
if (
fos != null) {
00177
fos.close();
00178
fos = null;
00179 }
00180
if (
file != null) {
00181
final FileInputStream fis =
new FileInputStream(
file);
00182
final File f =
file;
00183
if (del) file = null;
00184
return new InputStream() {
00185
public int read()
throws IOException {
00186
return fis.read();
00187 }
00188
public int read(byte[] buf)
throws IOException {
00189
return fis.read(buf);
00190 }
00191
public int read(byte[] buf,
int off,
int cnt)
throws IOException {
00192
return fis.read(buf, off, cnt);
00193 }
00194
public void close()
throws IOException {
00195 fis.close();
00196
if (del) f.delete();
00197 }
00198
public void finalize() {
00199
try {
00200
close();
00201 }
catch (Throwable t) {}
00202 }
00203 };
00204 }
00205
throw new IOException(
"no data");
00206 }
00207
00208
00209
00210
00211 public String
toString() {
00212
if (
bos != null)
return bos.toString();
00213
if (
fos != null) {
00214
throw new RuntimeException(
"too large");
00215 }
00216
return null;
00217 }
00218
00219 public void close() {
00220
bos = null;
00221
if (
fos != null) {
00222
try {
00223
fos.close();
00224 }
catch (Exception e) {
00225 }
00226 }
00227
fos = null;
00228
if (
file != null) {
00229
try {
00230
file.delete();
00231 }
catch (Exception e) {
00232 }
00233 }
00234
file = null;
00235
len = 0;
00236 }
00237
00238 public void reset() {
00239
close();
00240 }
00241
00242 public void finalize() {
00243
close();
00244 }
00245 }