Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 280453 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/rse/services/files/DefaultFileServiceCodePageConverter.java (-24 / +75 lines)
Lines 15-20 Link Here
15
 * David McKnight    (IBM)  -[279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252
15
 * David McKnight    (IBM)  -[279014] [dstore][encoding] text file corruption can occur when downloading from UTF8 to cp1252
16
 * David McKnight    (IBM)  -[324669] [dstore] IBM-eucJP to UTF-8 char conversion appends nulls to end of file during text-mode download
16
 * David McKnight    (IBM)  -[324669] [dstore] IBM-eucJP to UTF-8 char conversion appends nulls to end of file during text-mode download
17
 * David McKnight    (IBM)  -[280451] IFileServiceCodePageConverter.convertClientStringToRemoteBytes() should throw runtime exception
17
 * David McKnight    (IBM)  -[280451] IFileServiceCodePageConverter.convertClientStringToRemoteBytes() should throw runtime exception
18
 * David McKnight    (IBM)  -[280453] [performance] DefaultFileServiceCodePageConverter is wasteful with main memory
18
 ********************************************************************************/
19
 ********************************************************************************/
19
package org.eclipse.rse.services.files;
20
package org.eclipse.rse.services.files;
20
21
Lines 55-89 Link Here
55
		// read in the file
56
		// read in the file
56
		try
57
		try
57
		{
58
		{
59
			// decoder to go from remote encoding to UTF8
60
			Charset rmtCharset = Charset.forName(remoteEncoding);
61
			CharsetDecoder rmtDecoder = rmtCharset.newDecoder();
62
63
			inputStream = new FileInputStream(file);
64
			
58
			int fileLength = (int)file.length();
65
			int fileLength = (int)file.length();
66
			BufferedInputStream bufInputStream = new BufferedInputStream(inputStream, fileLength);
67
			
68
59
			if (fileLength > 0){
69
			if (fileLength > 0){
60
				inputStream = new FileInputStream(file);
61
				BufferedInputStream bufInputStream = new BufferedInputStream(inputStream, fileLength);
62
				byte[] buffer = new byte[fileLength];
63
				bufInputStream.read(buffer, 0, fileLength);
64
				bufInputStream.close();
65
				ByteBuffer rmtBuf = ByteBuffer.wrap(buffer, 0, fileLength);
66
67
				// decoder to go from remote encoding to UTF8
68
				Charset rmtCharset = Charset.forName(remoteEncoding);
69
				CharsetDecoder rmtDecoder = rmtCharset.newDecoder();
70
71
				// convert from the remote encoding
72
				CharBuffer decodedBuf = null;
73
				decodedBuf = rmtDecoder.decode(rmtBuf);
74
				// for conversion to the local encoding
75
				Charset charset = Charset.forName(localEncoding);
76
				CharsetEncoder encoder = charset.newEncoder();
77
				byte[] localBuffer = null;
78
				// convert to the specified local encoding
79
				ByteBuffer lclBuf = encoder.encode(decodedBuf);
80
				localBuffer = lclBuf.array();
81
				outStream = new FileOutputStream(file);
82
				
70
				
83
				// use the limit rather than the array length to avoid unwanted nulls
71
				int MAX_READ = 10000;
84
				outStream.write(localBuffer, 0, lclBuf.limit());
72
				if (fileLength <= MAX_READ){ // read the whole file at once
73
74
					byte[] buffer = new byte[fileLength];
75
					bufInputStream.read(buffer, 0, fileLength);
76
					bufInputStream.close();
77
					inputStream.close();
78
					
79
					ByteBuffer rmtBuf = ByteBuffer.wrap(buffer, 0, fileLength);
80
						
81
					// convert from the remote encoding
82
					CharBuffer decodedBuf = null;
83
					decodedBuf = rmtDecoder.decode(rmtBuf);
84
					// for conversion to the local encoding
85
					Charset charset = Charset.forName(localEncoding);
86
					CharsetEncoder encoder = charset.newEncoder();
87
					byte[] localBuffer = null;
88
					// convert to the specified local encoding
89
					ByteBuffer lclBuf = encoder.encode(decodedBuf);
90
					localBuffer = lclBuf.array();
91
92
					// use the limit rather than the array length to avoid unwanted nulls
93
					outStream = new FileOutputStream(file);
94
					outStream.write(localBuffer, 0, lclBuf.limit());
95
				}
96
				else { // read and write sections of file at a time
97
					int inOffset = 0;
98
					int outOffset = 0;
99
					
100
					File altFile = new File(file.getAbsolutePath() + "~"); //$NON-NLS-1$
101
					outStream = new FileOutputStream(altFile); // using alternate file because we're writing while reading
102
					while (inOffset < fileLength){
103
						int readSize = MAX_READ;
104
						if (inOffset +  MAX_READ > fileLength){
105
							readSize = fileLength - inOffset;
106
						}
107
						
108
						byte[] buffer = new byte[readSize];
109
						inputStream.read(buffer, 0, readSize);
110
						inOffset += readSize;
111
						ByteBuffer rmtBuf = ByteBuffer.wrap(buffer, 0, readSize);		
112
						
113
						// convert from the remote encoding
114
						CharBuffer decodedBuf = null;
115
						decodedBuf = rmtDecoder.decode(rmtBuf);
116
117
						// for conversion to the local encoding
118
						Charset charset = Charset.forName(localEncoding);
119
						CharsetEncoder encoder = charset.newEncoder();
120
						byte[] localBuffer = null;
121
						// convert to the specified local encoding
122
						ByteBuffer lclBuf = encoder.encode(decodedBuf);
123
						localBuffer = lclBuf.array();
124
											
125
						// use the limit rather than the array length to avoid unwanted nulls
126
						int writeSize = lclBuf.limit();
127
						outStream.write(localBuffer, 0, writeSize);
128
						outOffset += writeSize;
129
					}
130
					inputStream.close();	
131
					outStream.close();
132
					altFile.renameTo(file);
133
				}
134
85
			}
135
			}
86
		} catch (Exception e) {
136
		} catch (Exception e) {
137
			e.printStackTrace();
87
			// outstream could not be written properly: report
138
			// outstream could not be written properly: report
88
			throw new RuntimeException(e);
139
			throw new RuntimeException(e);
89
		} finally {
140
		} finally {

Return to bug 280453