[BAEL-7043] mutable strings (#15112)

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2023-11-02 00:32:13 +05:30 committed by GitHub
parent 33f52b2e0b
commit aaa4e333a6
5 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.mutablestrings;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
public class CharsetUsageExample {
public ByteBuffer encodeString(String inputString) {
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(inputString);
ByteBuffer byteBuffer = ByteBuffer.allocate(50);
encoder.encode(charBuffer, byteBuffer, true); // true indicates the end of input
byteBuffer.flip();
return byteBuffer;
}
public String decodeString(ByteBuffer byteBuffer) {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer decodedCharBuffer = CharBuffer.allocate(50);
decoder.decode(byteBuffer, decodedCharBuffer, true);
decodedCharBuffer.flip();
return decodedCharBuffer.toString();
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.mutablestrings;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.concurrent.atomic.AtomicReference;
public class MutableStringUsingCharset {
private final AtomicReference<CharBuffer> cbRef = new AtomicReference<>();
private final Charset myCharset = new Charset("mycharset", null) {
@Override
public boolean contains(Charset cs) {
return false;
}
@Override
public CharsetDecoder newDecoder() {
return new CharsetDecoder(this, 1.0f, 1.0f) {
@Override
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
cbRef.set(out);
while (in.remaining() > 0) {
out.append((char) in.get());
}
return CoderResult.UNDERFLOW;
}
};
}
@Override
public CharsetEncoder newEncoder() {
CharsetEncoder cd = new CharsetEncoder(this, 1.0f, 1.0f) {
@Override
protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
while (in.hasRemaining()) {
if (!out.hasRemaining()) {
return CoderResult.OVERFLOW;
}
char currentChar = in.get();
if (currentChar > 127) {
return CoderResult.unmappableForLength(1);
}
out.put((byte) currentChar);
}
return CoderResult.UNDERFLOW;
}
};
return cd;
}
};
public String createModifiableString(String s) {
return new String(s.getBytes(), myCharset);
}
public void modifyString() {
CharBuffer cb = cbRef.get();
cb.position(0);
cb.put("xyz");
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.mutablestrings;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import com.google.errorprone.annotations.DoNotCall;
public class MutableStrings {
/**
* This involves using Reflection to change String fields and it is not encouraged to use this in programs.
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
@DoNotCall
public void mutableUsingReflection() throws NoSuchFieldException, IllegalAccessException {
String myString = "Hello World";
String otherString = new String("Hello World");
Field f = String.class.getDeclaredField("value");
f.setAccessible(true);
f.set(myString, "Hi World".toCharArray());
System.out.println(otherString);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.mutablestring;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.baeldung.mutablestrings.CharsetUsageExample;
public class CharsetUsageUnitTest {
@Test
public void givenCharset_whenStringIsEncodedAndDecoded_thenGivesCorrectResult() {
CharsetUsageExample ch = new CharsetUsageExample();
String inputString = "hello दुनिया";
String result = ch.decodeString(ch.encodeString(inputString));
Assertions.assertEquals(inputString, result);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.mutablestring;
import org.junit.Assert;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import com.baeldung.mutablestrings.MutableStringUsingCharset;
public class MutableStringUsingCharsetUnitTest {
@Test
@Disabled
/**
* This test is disabled as it works well for Java 8 and below
*/
public void givenCustomCharSet_whenStringUpdated_StringGetsMutated() throws Exception {
MutableStringUsingCharset ms = new MutableStringUsingCharset();
String s = ms.createModifiableString("Hello");
Assert.assertEquals("Hello", s);
ms.modifyString();
Assert.assertEquals("something", s);
}
}