Skip to content

Commit b9694e5

Browse files
committed
Bug 573797: Refactor code to use Charset instead of passing around Strings
This simplifies error handling as once you have a Charset you don't have to worry about whether or not an UnsupportedEncodingException can be thrown anymore. In addition it is a little easier on type safety. Change-Id: I4292878a7c621f9d05fdb98f5c26a0ae8bfec062
1 parent d3d3410 commit b9694e5

File tree

4 files changed

+92
-29
lines changed

4 files changed

+92
-29
lines changed

terminal/plugins/org.eclipse.tm.terminal.control/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.tm.terminal.control; singleton:=true
5-
Bundle-Version: 5.2.100.qualifier
5+
Bundle-Version: 5.3.0.qualifier
66
Bundle-Activator: org.eclipse.tm.internal.terminal.control.impl.TerminalPlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin

terminal/plugins/org.eclipse.tm.terminal.control/src/org/eclipse/tm/internal/terminal/control/ITerminalViewControl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.eclipse.tm.internal.terminal.control;
1818

1919
import java.io.UnsupportedEncodingException;
20+
import java.nio.charset.Charset;
2021

2122
import org.eclipse.swt.dnd.Clipboard;
2223
import org.eclipse.swt.graphics.Font;
@@ -38,18 +39,38 @@ public interface ITerminalViewControl {
3839
*
3940
* @see ITerminalControl#setEncoding(String)
4041
* @since org.eclipse.tm.terminal 2.0
42+
* @deprecated Use {@link #setCharset(Charset)} and do the error handling in the UI code.
4143
*/
44+
@Deprecated
4245
void setEncoding(String encoding) throws UnsupportedEncodingException;
4346

47+
/**
48+
* Set the charset that the Terminal uses to decode byte streams into
49+
* characters.
50+
*
51+
* @see ITerminalControl#setCharset(Charset)
52+
* @since 5.3
53+
*/
54+
void setCharset(Charset charset);
55+
4456
/**
4557
* Get the Terminal's current encoding.
4658
*
4759
* @return the current Encoding of the Terminal.
4860
* @see ITerminalControl#getEncoding()
4961
* @since org.eclipse.tm.terminal 2.0
62+
* @deprecated Use {@link #getCharset()} and call {@link Charset#name()} on the result
5063
*/
64+
@Deprecated
5165
String getEncoding();
5266

67+
/**
68+
* @return the non-<code>null</code> current Charset of the Terminal.
69+
* @see ITerminalControl#getCharset()
70+
* @since 5.3
71+
*/
72+
Charset getCharset();
73+
5374
boolean isEmpty();
5475

5576
/**

terminal/plugins/org.eclipse.tm.terminal.control/src/org/eclipse/tm/internal/terminal/emulator/VT100TerminalControl.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
156156
private boolean connectOnEnterIfClosed = true;
157157

158158
PipedInputStream fInputStream;
159-
private static final String defaultEncoding = Charset.defaultCharset().name();
160-
private String fEncoding = defaultEncoding;
159+
private Charset fCharset = Charset.defaultCharset();
161160
private InputStreamReader fInputStreamReader;
162161

163162
private ICommandInputField fCommandInputField;
@@ -230,38 +229,43 @@ public VT100TerminalControl(ITerminalListener target, Composite wndParent, ITerm
230229
fTerminalModel.setMaxHeight(1000);
231230
fInputStream = new PipedInputStream(8 * 1024);
232231
fTerminalText = new VT100Emulator(fTerminalModel, this, null);
233-
try {
234-
// Use Default Encoding as start, until setEncoding() is called
235-
setEncoding(null);
236-
} catch (UnsupportedEncodingException e) {
237-
// Should never happen
238-
e.printStackTrace();
239-
// Fall back to local Platform Default Encoding
240-
fEncoding = defaultEncoding;
241-
fInputStreamReader = new InputStreamReader(fInputStream);
242-
fTerminalText.setInputStreamReader(fInputStreamReader);
243-
}
244-
232+
// Use Default Charset as start, until setCharset() is called
233+
setCharset(Charset.defaultCharset());
245234
setupTerminal(wndParent);
246235
}
247236

248237
@Override
238+
@Deprecated
249239
public void setEncoding(String encoding) throws UnsupportedEncodingException {
240+
Charset charset;
250241
if (encoding == null) {
251-
// TODO better use a standard remote-to-local encoding?
252-
encoding = "ISO-8859-1"; //$NON-NLS-1$
253-
// TODO or better use the local default encoding?
254-
// encoding = defaultEncoding;
242+
charset = Charset.defaultCharset();
243+
} else {
244+
charset = Charset.forName(encoding);
255245
}
256-
fInputStreamReader = new InputStreamReader(fInputStream, encoding);
257246
// remember encoding if above didn't throw an exception
258-
fEncoding = encoding;
247+
setCharset(charset);
248+
}
249+
250+
@Override
251+
public void setCharset(Charset charset) {
252+
if (charset == null) {
253+
charset = Charset.defaultCharset();
254+
}
255+
fInputStreamReader = new InputStreamReader(fInputStream, charset);
256+
fCharset = charset;
259257
fTerminalText.setInputStreamReader(fInputStreamReader);
260258
}
261259

262260
@Override
261+
@Deprecated
263262
public String getEncoding() {
264-
return fEncoding;
263+
return fCharset.name();
264+
}
265+
266+
@Override
267+
public Charset getCharset() {
268+
return fCharset;
265269
}
266270

267271
@Override
@@ -547,7 +551,7 @@ protected void sendString(String string) {
547551
// TODO: Find a way to force this to use the ISO Latin-1 encoding.
548552
// TODO: handle Encoding Errors in a better way
549553

550-
getOutputStream().write(string.getBytes(fEncoding));
554+
getOutputStream().write(string.getBytes(fCharset));
551555
getOutputStream().flush();
552556
} catch (SocketException socketException) {
553557
displayTextInTerminal(socketException.getMessage());
@@ -591,7 +595,7 @@ protected void sendChar(char chKey, boolean altKeyPressed) {
591595
//
592596
// TODO: Make the ESCAPE-vs-highbit behavior user configurable.
593597

594-
byte[] bytesToSend = String.valueOf(chKey).getBytes(fEncoding);
598+
byte[] bytesToSend = String.valueOf(chKey).getBytes(fCharset);
595599
StringBuilder b = new StringBuilder("sending ESC"); //$NON-NLS-1$
596600
for (int i = 0; i < bytesToSend.length; i++) {
597601
if (i != 0)
@@ -602,7 +606,7 @@ protected void sendChar(char chKey, boolean altKeyPressed) {
602606
os.write('\u001b');
603607
os.write(bytesToSend);
604608
} else {
605-
byte[] bytesToSend = String.valueOf(chKey).getBytes(fEncoding);
609+
byte[] bytesToSend = String.valueOf(chKey).getBytes(fCharset);
606610
StringBuilder b = new StringBuilder("sending"); //$NON-NLS-1$
607611
for (int i = 0; i < bytesToSend.length; i++) {
608612
if (i != 0)
@@ -826,10 +830,7 @@ public void displayTextInTerminal(String text) {
826830

827831
private void writeToTerminal(String text) {
828832
try {
829-
getRemoteToTerminalOutputStream().write(text.getBytes(fEncoding));
830-
} catch (UnsupportedEncodingException e) {
831-
// should never happen!
832-
e.printStackTrace();
833+
getRemoteToTerminalOutputStream().write(text.getBytes(fCharset));
833834
} catch (IOException e) {
834835
// should never happen!
835836
e.printStackTrace();

terminal/plugins/org.eclipse.tm.terminal.control/src/org/eclipse/tm/internal/terminal/provisional/api/ITerminalControl.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.OutputStream;
2020
import java.io.UnsupportedEncodingException;
21+
import java.nio.charset.Charset;
2122

2223
import org.eclipse.swt.widgets.Composite;
2324
import org.eclipse.swt.widgets.Shell;
@@ -83,9 +84,39 @@ public interface ITerminalControl {
8384
* LANG on UNIX systems.
8485
*
8586
* @since org.eclipse.tm.terminal 2.0
87+
* @deprecated Use {@link #setCharset(Charset)} and do the error handling in the UI code.
8688
*/
89+
@Deprecated
8790
void setEncoding(String encoding) throws UnsupportedEncodingException;
8891

92+
/**
93+
* Set the charset that the Terminal uses to decode bytes from the
94+
* Terminal-to-remote-Stream into Unicode Characters used in Java; or, to
95+
* encode Characters typed by the user into bytes sent over the wire to the
96+
* remote.
97+
*
98+
* By default, the local Platform Default charset is used. Also note that
99+
* the encoding must not be applied in case the terminal stream is processed
100+
* by some data transfer protocol which requires binary data.
101+
*
102+
* Validity of the charset set here is not checked. Since some encodings do
103+
* not cover the entire range of Unicode characters, it can happen that a
104+
* particular Unicode String typed in by the user can not be encoded into a
105+
* byte Stream with the encoding specified. and UnsupportedEncodingException
106+
* will be thrown in this case at the time the String is about to be
107+
* processed.
108+
*
109+
* The concrete encoding to use can either be specified manually by a user,
110+
* by means of a dialog, or a connector can try to obtain it automatically
111+
* from the remote side e.g. by evaluating an environment variable such as
112+
* LANG on UNIX systems.
113+
*
114+
* @param charset Charset to use, or <code>null</code> for platform's default charset.
115+
*
116+
* @since 5.3
117+
*/
118+
void setCharset(Charset charset);
119+
89120
/**
90121
* Return the current encoding. That's interesting when the previous
91122
* setEncoding() call failed and the fallback default encoding should be
@@ -94,9 +125,19 @@ public interface ITerminalControl {
94125
*
95126
* @return the current Encoding of the Terminal.
96127
* @since org.eclipse.tm.terminal 2.0
128+
* @deprecated Use {@link #getCharset()} and call {@link Charset#name()} on the result
97129
*/
130+
@Deprecated
98131
String getEncoding();
99132

133+
/**
134+
* Return the current charset.
135+
*
136+
* @return the non-<code>null</code> current charset of the Terminal
137+
* @since 5.3
138+
*/
139+
Charset getCharset();
140+
100141
/**
101142
* Show a text in the terminal. If puts newlines at the beginning and the
102143
* end.

0 commit comments

Comments
 (0)