001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud;
023:
024: import gruntspud.connection.ConnectionProfile;
025:
026: import java.io.BufferedInputStream;
027: import java.io.BufferedOutputStream;
028: import java.io.File;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033:
034: import org.netbeans.lib.cvsclient.file.WriteTextFilePreprocessor;
035:
036: public class GruntspudWriteTextFilePreprocessor implements
037: WriteTextFilePreprocessor {
038:
039: private static final int BUFFER_SIZE = 32768;
040: private static String DEFAULT_LINE_SEPARATOR = System
041: .getProperty("line.separator");
042:
043: private final static int CR = 1;
044: private final static int CR_LF = 2;
045: private final static int LF_CR = 3;
046: private final static int UNKNOWN = 0;
047:
048: private ConnectionProfile profile;
049: private GruntspudContext context;
050:
051: public GruntspudWriteTextFilePreprocessor(
052: ConnectionProfile profile, GruntspudContext context) {
053: this .profile = profile;
054: this .context = context;
055: }
056:
057: public void copyTextFileToLocation(InputStream pin, File file)
058: throws IOException {
059: InputStream in = null;
060: OutputStream out = null;
061: OutputStream debugOut = null;
062: byte[] line = getSeparatorSequence().getBytes();
063: Constants.CVS_LOG.debug("Using line separator "
064: + debugSequence(getSeparatorSequence()) + " to "
065: + file.getAbsolutePath());
066: try {
067: in = new BufferedInputStream(pin);
068: out = new BufferedOutputStream(new FileOutputStream(file));
069: if (context.getHost().getBooleanProperty(
070: Constants.OPTIONS_SYSTEM_LOG_CVS_IO, false)) {
071: File debugFile = new File(System
072: .getProperty("user.home")
073: + File.separator + "GRUNTSPUD.RECEIVED");
074: Constants.IO_LOG.debug("Writing last received file to "
075: + debugFile.getAbsolutePath());
076: debugOut = new FileOutputStream(debugFile);
077: }
078:
079: byte[] b = new byte[BUFFER_SIZE];
080: int l = 0;
081: int type = 0;
082: for (int z = in.read(b); z > 0; z = in.read(b)) {
083: if (debugOut != null) {
084: debugOut.write(b, 0, z);
085: }
086:
087: if (line.length == 0) {
088: out.write(b, 0, z);
089: } else {
090: for (int i = 0; i < z; i++) {
091: if (b[i] == 10) {
092: if (l != 13) {
093: out.write(line);
094: }
095: l = b[i];
096: } else {
097: if (b[i] == 13) {
098: out.write(line);
099: } else {
100: out.write(b[i]);
101: }
102: }
103: l = b[i];
104: }
105: }
106: }
107: out.flush();
108: if (debugOut != null) {
109: debugOut.flush();
110: }
111: } finally {
112: GruntspudUtil.closeStream(in);
113: GruntspudUtil.closeStream(out);
114: GruntspudUtil.closeStream(debugOut);
115: }
116: }
117:
118: private String debugSequence(String seq) {
119: StringBuffer buf = new StringBuffer();
120: for (int i = 0; i < seq.length(); i++) {
121: if (buf.length() > 0) {
122: buf.append(",");
123: }
124: buf.append(Integer.toHexString(seq.charAt(i)));
125: }
126: return buf.toString();
127: }
128:
129: public String getSeparatorSequence() {
130: switch (profile.getLineEndings()) {
131: case ConnectionProfile.UNIX_LINE_ENDINGS:
132: return "\n";
133: case ConnectionProfile.WINDOWS_LINE_ENDINGS:
134: return "\r\n";
135: case ConnectionProfile.IGNORE_LINE_ENDINGS:
136: return "";
137: default:
138: return DEFAULT_LINE_SEPARATOR;
139: }
140: }
141:
142: }
|