001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /* *************************************************************************
021: *
022: * Copyright (c) 2002, SeeBeyond Technology Corporation,
023: * All Rights Reserved
024: *
025: * This program, and all the routines referenced herein,
026: * are the proprietary properties and trade secrets of
027: * SEEBEYOND TECHNOLOGY CORPORATION.
028: *
029: * Except as provided for by license agreement, this
030: * program shall not be duplicated, used, or disclosed
031: * without written consent signed by an officer of
032: * SEEBEYOND TECHNOLOGY CORPORATION.
033: *
034: ***************************************************************************/
035: package org.netbeans.modules.sql.project.wsdl;
036:
037: import java.io.FileOutputStream;
038: import java.io.BufferedInputStream;
039: import java.io.BufferedOutputStream;
040: import java.io.File;
041: import java.io.ByteArrayInputStream;
042: import java.io.ByteArrayOutputStream;
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.io.InputStreamReader;
046: import java.io.OutputStream;
047: import java.io.OutputStreamWriter;
048: import java.io.Reader;
049: import java.io.StringWriter;
050: import java.io.Writer;
051: import java.util.Enumeration;
052: import java.util.zip.ZipFile;
053: import java.util.zip.ZipEntry;
054:
055: public class IOUtil {
056: private static final java.util.logging.Logger mLog = java.util.logging.Logger
057: .getLogger("com.sun.jbi.ui.devtool.tcg.util");
058:
059: private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
060:
061: // UTF-8
062: public static void encode(InputStream input, String srcEncoding,
063: OutputStream output, String targetEncoding)
064: throws IOException {
065: Reader in = new InputStreamReader(input, srcEncoding);
066: Writer out = new OutputStreamWriter(output, targetEncoding);
067: copy(in, out);
068: }
069:
070: // UTF-8
071: public static byte[] encode(byte[] srcBuf, String srcEncoding,
072: String targetEncoding) throws IOException {
073: String s = new String(srcBuf, srcEncoding);
074: byte[] ret = s.getBytes(targetEncoding);
075: return ret;
076: }
077:
078: /**
079: * Copys the input stream to the output stream
080: *
081: * @param input the input stream
082: * @param output the output stream
083: * @exception IOException Description of the Exception
084: */
085: public static void copy(InputStream input, OutputStream output)
086: throws IOException {
087: byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
088: int n = 0;
089: while ((n = input.read(buf)) != -1) {
090: output.write(buf, 0, n);
091: }
092: output.flush();
093: }
094:
095: /**
096: * Copys the input bytes to the output stream
097: *
098: * @param input the input bytes
099: * @param output the output stream
100: * @exception IOException Description of the Exception
101: */
102: public static void copy(byte[] input, OutputStream output)
103: throws IOException {
104: ByteArrayInputStream in = new ByteArrayInputStream(input);
105: copy(in, output);
106: }
107:
108: /**
109: * Copys the input stream to the output stream
110: *
111: * @param input the input stream
112: * @param output the output stream
113: * @exception IOException Description of the Exception
114: */
115: public static void copy(Reader input, Writer output)
116: throws IOException {
117: char[] buf = new char[DEFAULT_BUFFER_SIZE];
118: int n = 0;
119: while ((n = input.read(buf)) != -1) {
120: output.write(buf, 0, n);
121: }
122: output.flush();
123: }
124:
125: /**
126: * Copys the input bytes to the output stream
127: *
128: * @param input the input bytes
129: * @param output the output stream
130: * @exception IOException Description of the Exception
131: */
132: public static void copy(byte[] input, Writer output)
133: throws IOException {
134: ByteArrayInputStream in = new ByteArrayInputStream(input);
135: copy(in, output);
136: }
137:
138: /**
139: * Copys the input stream to the output stream
140: *
141: * @param input the input stream
142: * @param output the output stream
143: * @exception IOException Description of the Exception
144: */
145: public static void copy(InputStream input, Writer output)
146: throws IOException {
147: InputStreamReader in = new InputStreamReader(input);
148: copy(in, output);
149: }
150:
151: /**
152: * Copys the input stream to the output stream
153: *
154: * @param input the input stream
155: * @param output the output stream
156: * @exception IOException Description of the Exception
157: */
158: public static void copy(Reader input, OutputStream output)
159: throws IOException {
160: OutputStreamWriter out = new OutputStreamWriter(output);
161: copy(input, out);
162: }
163:
164: /**
165: * Returns the contents of the input stream as a String
166: *
167: * @param input the input stream
168: * @return The text value
169: * @exception IOException Description of the Exception
170: */
171: /*public static String getText(InputStream input)
172: throws IOException {
173: StringWriter out = new StringWriter();
174: copy(input, out);
175: String ret = out.toString();
176: //mLog.debug("ret: " + ret);
177: return ret;
178: }*/
179:
180: // UTF-8
181: public static String getText(InputStream input, String srcEncoding)
182: throws IOException {
183: InputStreamReader in = new InputStreamReader(input, srcEncoding);
184: StringWriter out = new StringWriter();
185: copy(in, out);
186: String ret = out.toString();
187: //mLog.debug("ret: " + ret);
188: return ret;
189: }
190:
191: public static String getText(String name, String srcEncoding)
192: throws IOException {
193: InputStream inputStream = null;
194: String ret = null;
195: try {
196: inputStream = getResourceAsStream(name);
197: ret = getText(inputStream, srcEncoding);
198: } catch (IOException e) {
199: throw e;
200: } finally {
201: if (inputStream != null) {
202: inputStream.close();
203: }
204: }
205: //mLog.debug("ret: " + ret);
206: return ret;
207: }
208:
209: /**
210: * Returns the contents of the input stream as a String
211: *
212: * @param input the input stream
213: * @return The text value
214: * @exception IOException Description of the Exception
215: */
216: public static String getText(Reader input) throws IOException {
217: StringWriter sw = new StringWriter();
218: copy(input, sw);
219: return sw.toString();
220: }
221:
222: /**
223: * Returns the contents of the input stream as a byte array
224: *
225: * @param input the input stream
226: * @return The byte array
227: * @exception IOException Description of the Exception
228: */
229: public static byte[] getBytes(InputStream input) throws IOException {
230: ByteArrayOutputStream output = new ByteArrayOutputStream();
231: copy(input, output);
232: return output.toByteArray();
233: }
234:
235: public static byte[] getBytes(String name) throws IOException {
236: return getBytes(getResourceAsStream(name));
237: }
238:
239: /**
240: * Returns the contents of the input stream as a byte array
241: *
242: * @param input the input stream
243: * @return The byte array
244: * @exception IOException Description of the Exception
245: */
246: public static byte[] getBytes(Reader input) throws IOException {
247: ByteArrayOutputStream output = new ByteArrayOutputStream();
248: copy(input, output);
249: return output.toByteArray();
250: }
251:
252: //==========================================================================
253: public static InputStream getResourceAsStream(String name) {
254: InputStream ret = null;
255: try {
256: if (new java.io.File(name).exists()) {
257: ret = new java.io.FileInputStream(name);
258: } else {
259: ret = new java.net.URL(name).openStream();
260: }
261: } catch (Exception e) {
262: ret = IOUtil.class.getClassLoader().getResourceAsStream(
263: name);
264: if (ret == null) {
265: mLog.warning("resource " + name + " not found");
266: } else {
267: //mLog.debug("IOUtil.getResourceAsStream " + name + " found");
268: }
269: }
270: return ret;
271: }
272:
273: public static Enumeration getResources() {
274: return null; // ClassLoader.getResources(name);
275: }
276:
277: public static void unzip(String zipFilePath, String targetDirPath)
278: throws IOException {
279: ZipFile zipFile = new ZipFile(zipFilePath);
280: for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
281: ZipEntry entry = (ZipEntry) e.nextElement();
282:
283: String path;
284: if (targetDirPath != null) {
285: path = targetDirPath + "/" + entry.getName();
286: } else {
287: path = entry.getName();
288: }
289:
290: if (entry.isDirectory()) {
291: File dir = new File(path);
292: if (!dir.exists()) {
293: dir.mkdirs();
294: }
295: } else {
296: InputStream input = new BufferedInputStream(zipFile
297: .getInputStream(entry));
298: OutputStream output = new BufferedOutputStream(
299: new FileOutputStream(path));
300: copy(input, output);
301: input.close();
302: output.close();
303: }
304: }
305: zipFile.close();
306: }
307:
308: public static final void main(String[] args) {
309: if (args.length != 2) {
310: System.err
311: .println("Usage: java IOUtil zipfile targetdirectory");
312: } else {
313: try {
314: unzip(args[0], args[1]);
315: } catch (Exception e) {
316: e.printStackTrace();
317: }
318: }
319: //mLog.debug("args " + args[0]);
320: //mLog.debug(IOUtil.class.getClassLoader().getResource(args[0]));
321: //mLog.debug(IOUtil.getResourceAsStream(args[0]));
322: }
323: }
|