1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-16 13:35:22 +02:00

[279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252

This commit is contained in:
Martin Oberhuber 2009-06-10 23:45:40 +00:00
parent 58c7aabfc5
commit 36bca66160
3 changed files with 91 additions and 31 deletions

View file

@ -56,6 +56,7 @@
* David McKnight (IBM) - [270468] [dstore] FileServiceSubSystem.list() returns folders when only FILE_TYPE_FILES is requested
* David McKnight (IBM) - [272335] [dstore] not handling case where upload fails
* David McKnight (IBM) - [278411] [dstore] upload status needs to be created in standard form when using windows server
* David McKnight (IBM) - [279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252
*******************************************************************************/
package org.eclipse.rse.internal.services.dstore.files;
@ -87,6 +88,7 @@ import org.eclipse.dstore.core.model.IDataStoreProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.rse.dstore.universal.miners.IUniversalDataStoreConstants;
import org.eclipse.rse.dstore.universal.miners.UniversalByteStreamHandler;
import org.eclipse.rse.internal.services.RSEServicesMessages;
import org.eclipse.rse.internal.services.dstore.Activator;
import org.eclipse.rse.internal.services.dstore.IDStoreMessageIds;
import org.eclipse.rse.internal.services.dstore.ServiceResources;
@ -822,8 +824,26 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
IFileServiceCodePageConverter codePageConverter = CodePageConverterManager.getCodePageConverter(encoding, this);
try {
codePageConverter.convertFileFromRemoteEncoding(remotePath, localFile, encoding, localEncoding, this);
}
catch (RuntimeException e){
Throwable ex = e.getCause();
StringBuffer msgTxtBuffer = new StringBuffer(RSEServicesMessages.FILEMSG_OPERATION_FAILED);
msgTxtBuffer.append('\n');
msgTxtBuffer.append('\n');
msgTxtBuffer.append(remotePath);
msgTxtBuffer.append('\n');
msgTxtBuffer.append(encoding);
msgTxtBuffer.append(" -> ");
msgTxtBuffer.append(localEncoding);
SystemMessage msg = new SimpleSystemMessage(Activator.PLUGIN_ID,
IDStoreMessageIds.FILEMSG_IO_ERROR,
IStatus.ERROR, msgTxtBuffer.toString(), ex);
throw new SystemMessageException(msg);
}
}
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION))
{
@ -1051,8 +1071,26 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
String localEncoding = SystemEncodingUtil.getInstance().getLocalDefaultEncoding();
IFileServiceCodePageConverter codePageConverter = CodePageConverterManager.getCodePageConverter(hostEncodings[i], this);
try {
codePageConverter.convertFileFromRemoteEncoding(remoteElement.getName(), localFile, hostEncodings[i], localEncoding, this);
}
catch (RuntimeException e){
Throwable ex = e.getCause();
StringBuffer msgTxtBuffer = new StringBuffer(RSEServicesMessages.FILEMSG_OPERATION_FAILED);
msgTxtBuffer.append('\n');
msgTxtBuffer.append('\n');
msgTxtBuffer.append(remoteFiles[i]);
msgTxtBuffer.append('\n');
msgTxtBuffer.append(hostEncodings[i]);
msgTxtBuffer.append(" -> ");
msgTxtBuffer.append(localEncoding);
SystemMessage msg = new SimpleSystemMessage(Activator.PLUGIN_ID,
IDStoreMessageIds.FILEMSG_IO_ERROR,
IStatus.ERROR, msgTxtBuffer.toString(), ex);
throw new SystemMessageException(msg);
}
}
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION))
{

View file

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2007, 2008 IBM Corporation. All rights reserved.
* Copyright (c) 2007, 2009 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
@ -12,6 +12,7 @@
* David McKnight (IBM) -[209704] [api][dstore] Ability to override default encoding conversion needed.
* David McKnight (IBM) -[220379] [api] Provide a means for contributing custom BIDI encodings
* David McKnight (IBM) -[246857] Rename problem when a file is opened in the editor
* David McKnight (IBM) -[279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252
********************************************************************************/
package org.eclipse.rse.services.files;
@ -20,6 +21,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
/**
* @since 3.0
@ -51,30 +57,43 @@ public class DefaultFileServiceCodePageConverter implements
inputStream = new FileInputStream(file);
BufferedInputStream bufInputStream = new BufferedInputStream(inputStream, fileLength);
byte[] buffer = new byte[fileLength];
int bytesRead = bufInputStream.read(buffer, 0, fileLength);
bufInputStream.read(buffer, 0, fileLength);
bufInputStream.close();
inputStream.close();
ByteBuffer rmtBuf = ByteBuffer.wrap(buffer, 0, fileLength);
byte[] localBuffer = new String(buffer, 0, bytesRead, remoteEncoding).getBytes(localEncoding);
// decoder to go from remote encoding to UTF8
Charset rmtCharset = Charset.forName(remoteEncoding);
CharsetDecoder rmtDecoder = rmtCharset.newDecoder();
// convert from the remote encoding
CharBuffer decodedBuf = null;
decodedBuf = rmtDecoder.decode(rmtBuf);
// for conversion to the local encoding
Charset charset = Charset.forName(localEncoding);
CharsetEncoder encoder = charset.newEncoder();
byte[] localBuffer = null;
// convert to the specified local encoding
ByteBuffer lclBuf = encoder.encode(decodedBuf);
localBuffer = lclBuf.array();
outStream = new FileOutputStream(file);
outStream.write(localBuffer, 0, localBuffer.length);
outStream.close();
}
}
catch (Exception e)
{
try {
} catch (Exception e) {
// outstream could not be written properly: report
throw new RuntimeException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ioe) {
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException ioe) {
}
}
catch (IOException ioe){
}
}
}

View file

@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2007, 2008 IBM Corporation. All rights reserved.
* Copyright (c) 2007, 2009 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
@ -11,11 +11,13 @@
* Contributors:
* David McKnight (IBM) -[209704] [api] Ability to override default encoding conversion needed.
* David McKnight (IBM) -[220379] [api] Provide a means for contributing custom BIDI encodings
* David McKnight (IBM) -[279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252
********************************************************************************/
package org.eclipse.rse.services.files;
import java.io.File;
import java.nio.charset.CharacterCodingException;
/**
@ -44,6 +46,7 @@ public interface IFileServiceCodePageConverter {
* @param localEncoding The remote encoding of the file
* @param fs The file service to apply conversion to.
* Can be used to determine implementation specific settings to the converter
* @throws RuntimeException (wrapping a CharacterCodingException or IOException) in case of an error transposing from source to target encoding
*/
public void convertFileFromRemoteEncoding(String remotePath, File file, String remoteEncoding, String localEncoding, IFileService fs);