Skip to content

Commit aab8d8f

Browse files
committed
use UTF-8 for readString() and write() in net client
1 parent 556ccb3 commit aab8d8f

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.*;
3030
import java.lang.reflect.*;
3131
import java.net.*;
32+
import java.nio.charset.StandardCharsets;
3233

3334
/**
3435
*
@@ -596,11 +597,10 @@ public int readBytesUntil(int interesting, byte byteBuffer[]) {
596597

597598
/**
598599
*
599-
* Returns the all the data from the buffer as a <b>String</b>. This method
600-
* assumes the incoming characters are ASCII. If you want to transfer
601-
* Unicode data, first convert the String to a byte stream in the
602-
* representation of your choice (i.e. UTF8 or two-byte Unicode data), and
603-
* send it as a byte array.
600+
* Returns the all the data from the buffer as a <b>String</b>.
601+
*
602+
* In 4.0 beta 3, changed to using UTF-8 as the encoding,
603+
* otherwise the behavior is platform-dependent.
604604
*
605605
* @webref client
606606
* @usage application
@@ -609,7 +609,7 @@ public int readBytesUntil(int interesting, byte byteBuffer[]) {
609609
public String readString() {
610610
byte b[] = readBytes();
611611
if (b == null) return null;
612-
return new String(b);
612+
return new String(b, StandardCharsets.UTF_8);
613613
}
614614

615615

@@ -619,10 +619,8 @@ public String readString() {
619619
* <b>null</b> if it doesn't find what you're looking for.
620620
*
621621
* <h3>Advanced</h3>
622-
* <p/>
623-
* If you want to move Unicode data, you can first convert the
624-
* String to a byte stream in the representation of your choice
625-
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
622+
* In 4.0 beta 3, changed to using UTF-8 as the encoding,
623+
* otherwise the behavior is platform-dependent.
626624
*
627625
* @webref client
628626
* @usage application
@@ -632,7 +630,7 @@ public String readString() {
632630
public String readStringUntil(int interesting) {
633631
byte b[] = readBytesUntil(interesting);
634632
if (b == null) return null;
635-
return new String(b);
633+
return new String(b, StandardCharsets.UTF_8);
636634
}
637635

638636

@@ -679,20 +677,11 @@ public void write(byte data[]) {
679677

680678

681679
/**
682-
* <h3>Advanced</h3>
683-
* Write a String to the output. Note that this doesn't account
684-
* for Unicode (two bytes per char), nor will it send UTF8
685-
* characters.. It assumes that you mean to send a byte buffer
686-
* (most often the case for networking and serial i/o) and
687-
* will only use the bottom 8 bits of each char in the string.
688-
* (Meaning that internally it uses String.getBytes)
689-
*
690-
* If you want to move Unicode data, you can first convert the
691-
* String to a byte stream in the representation of your choice
692-
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
680+
* In 4.0 beta 3, changed to using UTF-8 as the encoding,
681+
* otherwise the behavior is platform-dependent.
693682
*/
694683
public void write(String data) {
695-
write(data.getBytes());
684+
write(data.getBytes(StandardCharsets.UTF_8));
696685
}
697686

698687

todo.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ X skip fonts starting . and # because they're likely to confuse users
1212
X get rid of version numbers in the name of the batik.jar file
1313
X ffmpeg not downloading correctly on M1 machines
1414
X https://github.com/processing/processing4/issues/319
15+
X use UTF-8 for readString() and write() in net client
16+
X avoids platform-specific behavior; Java 18 also making UTF-8 the default
17+
X https://github.com/processing/processing4/issues/336
1518

1619
sam
1720
X Error when calling smooth() on PGraphics
@@ -30,8 +33,6 @@ before release
3033
_ disable Theme Engine from Tools (in build.xml)
3134

3235

33-
34-
3536
before beta 3
3637
_ Add support for localizing contributions
3738
_ https://github.com/processing/processing4/pull/237 (updated by Andres)

0 commit comments

Comments
 (0)