Source Code Cross Referenced for StreamPipe.java in  » Groupware » hipergate » com » knowgate » dfs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » hipergate » com.knowgate.dfs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.knowgate.dfs;
002:
003:        //-< Pipe.java >-----------------------------------------------------*--------*
004:        // JSYNC                      Version 1.04       (c) 1998  GARRET    *     ?  *
005:        // (Java synchronization classes)                                    *   /\|  *
006:        //                                                                   *  /  \  *
007:        //                          Created:     20-Jun-98    K.A. Knizhnik  * / [] \ *
008:        //                          Last update:  6-Jul-98    K.A. Knizhnik  * GARRET *
009:        // http://www.garret.ru/~knizhnik/java.html                                   *
010:        //-------------------------------------------------------------------*--------*
011:        // Link two existed input and output threads.
012:        //-------------------------------------------------------------------*--------*
013:
014:        import java.io.InputStream;
015:        import java.io.FileInputStream;
016:        import java.io.BufferedInputStream;
017:        import java.io.OutputStream;
018:        import java.io.IOException;
019:        import java.io.FileNotFoundException;
020:
021:        import com.knowgate.debug.DebugFile;
022:
023:        /** This class links input and output streams so that data taken from input
024:         *  stream is transfered to the output stream. This class can be used to
025:         *  connect standard input/ouput stream of Java application with
026:         *  output/input streams of spawned child process, so that all user's input is
027:         *  redirected to the child and all it's output is visible for the user.<P>
028:         *
029:         *  This class starts a thread, which transfers data from input stream to
030:         *  output stream until End Of File is reached or IOException caused by IO
031:         *  error is catched.
032:         * @author Konstantin Knizhnik
033:         * @version 1.04
034:         */
035:        public class StreamPipe {
036:            /** Default size of buffer used to transfer data from the input
037:             *  stream to the output stream.
038:             */
039:            private static final int defaultBufferSize = 8000;
040:            boolean bSynchronous;
041:
042:            /**
043:             * <b>Create synchronous stream conector.</b>
044:             * between() methods do not return until end of input stream is reached and
045:             * written into ouput stream.
046:             */
047:            public StreamPipe() {
048:                bSynchronous = true;
049:            }
050:
051:            /**
052:             * <b>Create synchronous or asynchronous stream conector.</b>
053:             * between() methods do not return until end of input stream is reached and
054:             * written into ouput stream.
055:             */
056:            public StreamPipe(boolean bSync) {
057:                bSynchronous = bSync;
058:            }
059:
060:            // -------------------------------------------------------------------------
061:
062:            private static void pipe(InputStream in, OutputStream out,
063:                    int bufferSize, boolean autoFlush) throws IOException {
064:
065:                if (DebugFile.trace) {
066:                    DebugFile.writeln("Begin StreamPipe.pipe()");
067:                }
068:
069:                int total = 0;
070:                byte[] buffer = new byte[bufferSize];
071:
072:                int length;
073:                while ((length = in.read(buffer)) > 0) {
074:                    if (DebugFile.trace)
075:                        DebugFile.writeln("OutputStream.write(byte[], 0, "
076:                                + String.valueOf(length) + ")");
077:                    out.write(buffer, 0, length);
078:                    if (autoFlush)
079:                        out.flush();
080:                    total += length;
081:                }
082:
083:                if (DebugFile.trace) {
084:                    DebugFile.writeln("End StreamPipe.pipe() : "
085:                            + String.valueOf(total));
086:                }
087:            } // pipe
088:
089:            // -------------------------------------------------------------------------
090:
091:            /** Establish connection between input and output streams with specified size of buffer used for data transfer.
092:             * @param in  input stream
093:             * @param out output stream
094:             * @param bufferSize size of buffer used to transfer data from the input stream to the output stream
095:             * @param  autoFlush if set to <b>true</b> OutputStream.flush() method will be called each time bufferSize bytes are written into output stream
096:             */
097:            public void between(InputStream in, OutputStream out,
098:                    int bufferSize, boolean autoFlush) throws IOException {
099:                if (bSynchronous)
100:                    pipe(in, out, bufferSize, autoFlush);
101:                else
102:                    (new PipeThread(in, out, bufferSize, autoFlush)).start();
103:            }
104:
105:            /** Establish connection between input and output streams with specified size of buffer used for data transfer and no auto-flush.
106:             * @param in  input stream
107:             * @param out output stream
108:             */
109:            public void between(InputStream in, OutputStream out, int bufferSize)
110:                    throws IOException {
111:                if (bSynchronous)
112:                    pipe(in, out, bufferSize, false);
113:                else
114:                    (new PipeThread(in, out, bufferSize, false)).start();
115:            }
116:
117:            /** Establish connection between input and output streams with default buffer size and no auto-flush.
118:             * @param in  input stream
119:             * @param out output stream
120:             */
121:            public void between(InputStream in, OutputStream out)
122:                    throws IOException {
123:
124:                if (bSynchronous)
125:                    pipe(in, out, defaultBufferSize, false);
126:                else
127:                    (new PipeThread(in, out, defaultBufferSize, false)).start();
128:            }
129:
130:            // -------------------------------------------------------------------------
131:
132:            /** Establish synchronous connection between a file and an output stream
133:             * with specified size of buffer used for data transfer.
134:             * autoFlush is set to <b>false</b> and buffer size is set to 8000 bytes.
135:             * @param sFilePath input stream
136:             * @param oOutStrm output stream
137:             * @since 3.0
138:             */
139:            public static void between(String sFilePath, OutputStream oOutStrm)
140:                    throws IOException, FileNotFoundException {
141:
142:                FileInputStream oFileIoStrm = new FileInputStream(sFilePath);
143:                BufferedInputStream oBfIoStrm = new BufferedInputStream(
144:                        oFileIoStrm, defaultBufferSize);
145:                pipe(oBfIoStrm, oOutStrm, 8000, false);
146:                oBfIoStrm.close();
147:                oFileIoStrm.close();
148:            } // between
149:        }
150:
151:        // ---------------------------------------------------------------------------
152:
153:        final class PipeThread extends Thread {
154:            InputStream in;
155:            OutputStream out;
156:            byte[] buffer;
157:            boolean flush;
158:
159:            PipeThread(InputStream in, OutputStream out, int bufferSize,
160:                    boolean autoFlush) {
161:                this .in = in;
162:                this .out = out;
163:                buffer = new byte[bufferSize];
164:                flush = autoFlush;
165:
166:            }
167:
168:            public void run() {
169:                if (DebugFile.trace) {
170:                    DebugFile.writeln("Begin StreamPipe.PipeThread.run()");
171:                }
172:
173:                int total = 0;
174:
175:                try {
176:                    int length;
177:                    while ((length = in.read(buffer)) > 0) {
178:                        if (DebugFile.trace)
179:                            DebugFile.writeln("OutputStream.write(byte[], 0, "
180:                                    + String.valueOf(length) + ")");
181:                        out.write(buffer, 0, length);
182:                        if (flush)
183:                            out.flush();
184:                        total += length;
185:                    }
186:
187:                } catch (IOException ex) {
188:                }
189:
190:                if (DebugFile.trace) {
191:                    DebugFile.writeln("End StreamPipe.PipeThread.run() : "
192:                            + String.valueOf(total));
193:                }
194:            }
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.