KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > gen > WriteIfDifferentOutputStream


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.modules.schema2beansdev.gen;
21
22 import java.util.*;
23 import java.io.*;
24
25 public class WriteIfDifferentOutputStream extends OutputStream {
26     private RandomAccessFile randFile;
27     private long initialRandFileLength;
28     private boolean justWrite = false;
29
30     public WriteIfDifferentOutputStream(RandomAccessFile randFile) throws IOException {
31         this.randFile = randFile;
32         this.initialRandFileLength = randFile.length();
33     }
34
35     public WriteIfDifferentOutputStream(String JavaDoc filename) throws IOException {
36         this(new RandomAccessFile(filename, "rw")); // NOI18N
37
}
38
39     public WriteIfDifferentOutputStream(File file) throws IOException {
40         this(new RandomAccessFile(file, "rw")); // NOI18N
41
}
42
43     public void write(int b) throws IOException {
44         if (justWrite) {
45             randFile.write(b);
46             return;
47         }
48         long fp = randFile.getFilePointer();
49         if (fp + 1 > initialRandFileLength) {
50             justWrite = true;
51             randFile.write(b);
52             return;
53         }
54         int fromFile = randFile.read();
55         if (fromFile != b) {
56             //System.out.println("different: fromFile="+fromFile+" b="+b);
57
randFile.seek(fp);
58             randFile.write(b);
59             justWrite = true;
60             return;
61         }
62     }
63
64     private byte[] writeBuf;
65     private int lastLen = -1;
66     public void write(byte[] b, int off, int len) throws IOException {
67         if (justWrite) {
68             randFile.write(b, off, len);
69             return;
70         }
71         long fp = randFile.getFilePointer();
72         if (fp + len > initialRandFileLength) {
73             justWrite = true;
74             randFile.write(b, off, len);
75             return;
76         }
77         if (len > lastLen) {
78             // Allocate a new buffer only if the last one wasn't big enough.
79
writeBuf = new byte[len];
80             lastLen = len;
81         }
82         randFile.read(writeBuf, 0, len);
83         for (int i = off, j = 0; j < len; ++i, ++j) {
84             if (writeBuf[j] != b[i]) {
85                 //System.out.println("different: i="+i+" j="+j);
86
randFile.seek(fp);
87                 randFile.write(b, off, len);
88                 justWrite = true;
89                 return;
90             }
91         }
92     }
93
94     /*
95       public void flush() throws IOException {
96       randFile.flush();
97       }
98     */

99
100     /**
101      * At this point, have we changed the file?
102      * It's possible to not change a file until close() is called.
103      */

104     public boolean isChanged() {
105         return justWrite;
106     }
107
108     public void close() throws IOException {
109         // Truncate it.
110
//System.out.println("truncating to "+randFile.getFilePointer());
111
if (randFile.getFilePointer() < randFile.length()) {
112             justWrite = true;
113             randFile.setLength(randFile.getFilePointer());
114         }
115         randFile.close();
116     }
117 }
118
Popular Tags