1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

dstore - deal with \r\n in upload(IInputStream...) same as upload(File...)

This commit is contained in:
David McKnight 2007-11-07 17:45:27 +00:00
parent 002b3f7d0a
commit 6c5f63201a
2 changed files with 55 additions and 2 deletions

View file

@ -1938,7 +1938,8 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
{ {
mode = IUniversalDataStoreConstants.TEXT_MODE; mode = IUniversalDataStoreConstants.TEXT_MODE;
} }
DStoreOutputStream outputStream = new DStoreOutputStream(getDataStore(), remotePath, getEncoding(monitor), mode);
DStoreOutputStream outputStream = new DStoreOutputStream(getDataStore(), remotePath, getEncoding(monitor), mode, unixStyle);
return outputStream; return outputStream;
} }

View file

@ -28,14 +28,29 @@ public class DStoreOutputStream extends OutputStream
private int _mode; private int _mode;
private boolean _firstWrite = true; private boolean _firstWrite = true;
private String _byteStreamHandlerId; private String _byteStreamHandlerId;
private String _localLineSep;
private String _targetLineSep;
private int _localLineSepLength;
public DStoreOutputStream(DataStore dataStore, String remotePath, String encoding, int mode) public DStoreOutputStream(DataStore dataStore, String remotePath, String encoding, int mode, boolean unixStyle)
{ {
_dataStore = dataStore; _dataStore = dataStore;
_remotePath = remotePath; _remotePath = remotePath;
_encoding = encoding; _encoding = encoding;
_mode = mode; _mode = mode;
_byteStreamHandlerId = UniversalByteStreamHandler.class.getName(); _byteStreamHandlerId = UniversalByteStreamHandler.class.getName();
// line separator of local machine
_localLineSep = System.getProperty("line.separator"); //$NON-NLS-1$
// line separator of remote machine
_targetLineSep = "\n"; //$NON-NLS-1$
if (!unixStyle) {
_targetLineSep = "\r\n"; //$NON-NLS-1$
}
_localLineSepLength = _localLineSep.length();
} }
@ -60,6 +75,9 @@ public class DStoreOutputStream extends OutputStream
if (_mode == IUniversalDataStoreConstants.TEXT_MODE) if (_mode == IUniversalDataStoreConstants.TEXT_MODE)
{ {
String tempStr = new String(b, 0, length); String tempStr = new String(b, 0, length);
tempStr = convertLineSeparators(tempStr);
b = tempStr.getBytes(_encoding); b = tempStr.getBytes(_encoding);
} }
if (_firstWrite) if (_firstWrite)
@ -75,13 +93,47 @@ public class DStoreOutputStream extends OutputStream
} }
} }
private String convertLineSeparators(String tempStr)
{
// if the line end characters of the local and remote machines are different, we need to replace them
if (!_localLineSep.equals(_targetLineSep)) {
int index = tempStr.indexOf(_localLineSep);
StringBuffer buf = new StringBuffer();
boolean lineEndFound = false;
int lastIndex = 0;
while (index != -1) {
buf = buf.append(tempStr.substring(lastIndex, index));
buf = buf.append(_targetLineSep);
if (!lineEndFound) {
lineEndFound = true;
}
lastIndex = index+_localLineSepLength;
index = tempStr.indexOf(_localLineSep, lastIndex);
}
if (lineEndFound) {
buf = buf.append(tempStr.substring(lastIndex));
tempStr = buf.toString();
}
}
return tempStr;
}
public void write(byte[] b) throws IOException public void write(byte[] b) throws IOException
{ {
if (_mode == IUniversalDataStoreConstants.TEXT_MODE) if (_mode == IUniversalDataStoreConstants.TEXT_MODE)
{ {
String tempStr = new String(b, 0, b.length); String tempStr = new String(b, 0, b.length);
tempStr = convertLineSeparators(tempStr);
b = tempStr.getBytes(_encoding); b = tempStr.getBytes(_encoding);
} }
if (_firstWrite) if (_firstWrite)