001: package com.openedit.util;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.OutputStream;
009:
010: /**
011: * Creation date: (9/11/2001 4:43:00 PM)
012: * @author: CSB
013: */
014: public class OutputFiller {
015: protected int fieldBufferSize = 1024;
016:
017: /**
018: * InputFlusher constructor comment.
019: */
020: public OutputFiller() {
021: super ();
022: }
023:
024: /**
025: * Creation date: (9/11/2001 4:55:34 PM)
026: * @param inBufferSize int
027: */
028: public OutputFiller(int inBufferSize) {
029: setBufferSize(inBufferSize);
030: }
031:
032: public void fill(java.io.InputStream in, java.io.OutputStream out)
033: throws java.io.IOException {
034: byte[] bytes = new byte[getBufferSize()];
035:
036: int iRead = -1;
037:
038: while (true) {
039: iRead = in.read(bytes);
040:
041: if (iRead != -1) {
042: out.write(bytes, 0, iRead);
043: } else {
044: break;
045: }
046: }
047: out.flush();
048: }
049:
050: public void fill(java.io.Reader in, java.io.Writer out)
051: throws java.io.IOException {
052: char[] bytes = new char[getBufferSize()];
053:
054: int iRead = -1;
055:
056: while (true) {
057: iRead = in.read(bytes);
058:
059: if (iRead != -1) {
060: out.write(bytes, 0, iRead);
061: } else {
062: break;
063: }
064:
065: }
066: out.flush();
067: }
068:
069: public void fill(File inSource, File inOut) throws IOException {
070: FileInputStream in = null;
071: try {
072: in = new FileInputStream(inSource);
073: inOut.getParentFile().mkdirs();
074: FileOutputStream out = new FileOutputStream(inOut);
075: try {
076: fill(in, out);
077: } finally {
078: FileUtils.safeClose(out);
079: }
080: } finally {
081: FileUtils.safeClose(in);
082: }
083: }
084:
085: /**
086: * Creation date: (9/11/2001 4:53:38 PM)
087: * @return int
088: */
089: public int getBufferSize() {
090: return fieldBufferSize;
091: }
092:
093: /**
094: * Creation date: (9/11/2001 4:53:38 PM)
095: * @param newBufferSize int
096: */
097: public void setBufferSize(int newBufferSize) {
098: fieldBufferSize = newBufferSize;
099: }
100:
101: /**
102: * @param inIn
103: */
104: public void close(InputStream inIn) {
105: if (inIn != null) {
106: try {
107: inIn.close();
108: } catch (IOException ex) {
109: //fail silently
110: }
111: }
112: }
113:
114: public void close(OutputStream inIn) {
115: if (inIn != null) {
116: try {
117: inIn.close();
118: } catch (IOException ex) {
119: //fail silently
120: }
121: }
122: }
123:
124: public void fill(InputStream inIn, File inOutput)
125: throws IOException {
126: inOutput.getParentFile().mkdirs();
127: FileOutputStream out = new FileOutputStream(inOutput);
128: try {
129: fill(inIn, out);
130: } finally {
131: close(out);
132: }
133: }
134: }
|