001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.core;
043:
044: import java.io.*;
045: import java.lang.ref.SoftReference;
046: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
047: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileUtil;
050:
051: /**
052: *
053: * @author Vladimir Kvashin
054: */
055: public class FileBufferFile extends AbstractFileBuffer {
056:
057: private SoftReference bytes;
058:
059: public FileBufferFile(File file) {
060: super (file);
061: }
062:
063: public String getText() throws IOException {
064: return new String(getBytes());
065: }
066:
067: public String getText(int start, int end) {
068: try {
069: byte[] b = getBytes();
070: if (end > b.length) {
071: new IllegalArgumentException("")
072: .printStackTrace(System.err);
073: end = b.length;
074: }
075: return new String(b, start, end - start);
076: } catch (IOException e) {
077: DiagnosticExceptoins.register(e);
078: return "";
079: }
080: }
081:
082: public synchronized byte[] getBytes() throws IOException {
083: byte[] b;
084: if (bytes != null) {
085: Object o = bytes.get();
086: if (o != null) {
087: return (byte[]) o;
088: }
089: }
090: // either bytes == null or bytes.get() == null
091: b = doGetBytes();
092: bytes = new SoftReference(b);
093: return b;
094: }
095:
096: private byte[] doGetBytes() throws IOException {
097: File file = getFile();
098: long length = file.length();
099: if (length > Integer.MAX_VALUE) {
100: new IllegalArgumentException("File is too large: "
101: + file.getAbsolutePath())
102: .printStackTrace(System.err); // NOI18N
103: }
104: byte[] readBytes = new byte[(int) length];
105: InputStream is = getInputStream();
106: try {
107: int offset = 0;
108: int numRead = 0;
109: while (offset < readBytes.length
110: && (numRead = is.read(readBytes, offset,
111: readBytes.length - offset)) >= 0) {
112: offset += numRead;
113: }
114: } finally {
115: is.close();
116: }
117: convertLSToLF(readBytes);
118: return readBytes;
119: }
120:
121: private static int convertLSToLF(byte[] bytes) {
122: int len = bytes.length;
123: int tgtOffset = 0;
124: short lsLen = 0;
125: int moveStart = 0;
126: int moveLen;
127: for (int i = 0; i < len; i++) {
128: if (bytes[i] == '\r') {
129: if (i + 1 < len && bytes[i + 1] == '\n') {
130: lsLen = 2;
131: } else {
132: lsLen = 1;
133: }
134: }
135: if (lsLen > 0) {
136: moveLen = i - moveStart;
137: if (moveLen > 0) {
138: if (tgtOffset != moveStart) {
139: System.arraycopy(bytes, moveStart, bytes,
140: tgtOffset, moveLen);
141: }
142: tgtOffset += moveLen;
143: }
144: bytes[tgtOffset++] = '\n';
145: moveStart += moveLen + lsLen;
146: i += lsLen - 1;
147: lsLen = 0;
148: }
149: }
150: moveLen = len - moveStart;
151: if (moveLen > 0) {
152: if (tgtOffset != moveStart) {
153: System.arraycopy(bytes, moveStart, bytes, tgtOffset,
154: moveLen);
155: }
156: tgtOffset += moveLen;
157: }
158: return tgtOffset;
159: }
160:
161: public InputStream getInputStream() throws IOException {
162: File file = getFile();
163: FileObject fo = FileUtil.toFileObject(FileUtil
164: .normalizeFile(file));
165: InputStream is;
166: if (fo != null) {
167: is = fo.getInputStream();
168: } else {
169: is = new FileInputStream(file);
170: }
171: return new BufferedInputStream(is, TraceFlags.BUF_SIZE);
172: }
173:
174: public int getLength() {
175: return (int) getFile().length();
176: }
177:
178: public boolean isFileBased() {
179: return true;
180: }
181:
182: public long lastModified() {
183: return getFile().lastModified();
184: }
185:
186: ////////////////////////////////////////////////////////////////////////////
187: // impl of SelfPersistent
188:
189: @Override
190: public void write(DataOutput output) throws IOException {
191: super .write(output);
192: }
193:
194: public FileBufferFile(DataInput input) throws IOException {
195: super(input);
196: }
197: }
|