KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backup > backupers > NativeCommandInputSource


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.backup.backupers;
23
24 import java.io.BufferedOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * This class defines a NativeCommandInputSource, which provides input to
31  * processes. Current implementation could be extended to use interfaces but
32  * this works for now.
33  *
34  * @author <a HREF="mailto:robert.hodges@continuent.com">Robert Hodges</a>
35  * @version 1.0
36  */

37 public class NativeCommandInputSource
38 {
39   String JavaDoc[] inputStringArray;
40   InputStream JavaDoc inputStream;
41
42   /**
43    * Creates a new <code>NativeCommandInputSource</code> object to read from a
44    * strings of arrays.
45    *
46    * @param arrayInput Array of string inputs.
47    */

48   public NativeCommandInputSource(String JavaDoc[] arrayInput)
49   {
50     this.inputStringArray = arrayInput;
51   }
52
53   /**
54    * Creates a new <code>NativeCommandInputSource</code> object to read from
55    * an InputStream.
56    *
57    * @param inputStream An open input stream
58    */

59   public NativeCommandInputSource(InputStream JavaDoc inputStream)
60   {
61     this.inputStream = inputStream;
62   }
63
64   /**
65    * Factory method to create an input source that reads arrays.
66    *
67    * @param inputArray Array of string inputs.
68    * @return Configured input source
69    */

70   public static NativeCommandInputSource createArrayInputSource(
71       String JavaDoc[] inputArray)
72   {
73     if (inputArray == null)
74       return null;
75     else
76       return new NativeCommandInputSource(inputArray);
77   }
78
79   /**
80    * Factory method to create an input source that reads arrays.
81    *
82    * @param inputStream An open InputStream containing input
83    * @return Configured input source
84    */

85   public static NativeCommandInputSource createInputStreamSource(
86       InputStream JavaDoc inputStream)
87   {
88     if (inputStream == null)
89       return null;
90     else
91       return new NativeCommandInputSource(inputStream);
92   }
93
94   /**
95    * Writes all available input to the native command process, returning when
96    * the input source is exhausted.
97    *
98    * @param outputStream Stream that feeds input to process
99    * @throws IOException Thrown if there is an IO error
100    */

101   public void write(OutputStream JavaDoc outputStream) throws IOException JavaDoc
102   {
103     if (inputStringArray != null)
104     {
105       // Write array input.
106
BufferedOutputStream JavaDoc feed = new BufferedOutputStream JavaDoc(outputStream);
107       for (int i = 0; i < inputStringArray.length; i++)
108       {
109         feed.write(inputStringArray[i].getBytes());
110         feed.write("\n".getBytes());
111         feed.flush();
112       }
113     }
114     else
115     {
116       // Write output from stream.
117
byte[] buff = new byte[1024];
118       int len = 0;
119       while ((len = inputStream.read(buff)) > 0)
120       {
121         outputStream.write(buff, 0, len);
122       }
123     }
124   }
125
126   /**
127    * Closes the input source.
128    *
129    * @throws IOException Thrown if there is an error during the close operation
130    */

131   public void close() throws IOException JavaDoc
132   {
133     if (inputStringArray != null)
134     {
135       // Do nothing
136
}
137     else
138     {
139       inputStream.close();
140     }
141   }
142 }
Popular Tags