KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > file > DefaultWriteTextFilePreprocessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.cvsclient.file;
21
22 import java.io.*;
23
24 /**
25  * @author Thomas Singer
26  * @version Sep 26, 2001
27  */

28 public class DefaultWriteTextFilePreprocessor
29         implements WriteTextFilePreprocessor {
30
31     private static final int CHUNK_SIZE = 32768;
32
33     public void copyTextFileToLocation(InputStream processedInputStream, File fileToWrite, OutputStreamProvider customOutput) throws IOException {
34         // Here we read the temp file in again, doing any processing required
35
// (for example, unzipping). We must not convert the bytes to characters
36
// because the file may not be written in the current encoding.
37
// We would corrupt it's content when characters would be written!
38
InputStream tempInput = null;
39         OutputStream out = null;
40         byte[] newLine = System.getProperty("line.separator").getBytes();
41         try {
42             tempInput = new BufferedInputStream(processedInputStream);
43             out = new BufferedOutputStream(customOutput.createOutputStream());
44             // this chunk is directly read from the temp file
45
byte[] cchunk = new byte[CHUNK_SIZE];
46             for (int readLength = tempInput.read(cchunk);
47                  readLength > 0;
48                  readLength = tempInput.read(cchunk)) {
49
50                 // we must perform our own newline conversion. The file will
51
// definitely have unix style CRLF conventions, so if we have
52
// a \n this code will write out a \n or \r\n as appropriate for
53
// the platform we are running on
54
for (int i = 0; i < readLength; i++) {
55                     if (cchunk[i] == '\n') {
56                         out.write(newLine);
57                     }
58                     else {
59                         out.write(cchunk[i]);
60                     }
61                 }
62             }
63         }
64         finally {
65             if (tempInput != null) {
66                 try {
67                     tempInput.close();
68                 }
69                 catch (IOException ex) {
70                     // ignore
71
}
72             }
73             if (out != null) {
74                 try {
75                     out.close();
76                 }
77                 catch (IOException ex) {
78                     // ignore
79
}
80             }
81         }
82     }
83 }
84
Popular Tags