GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Prepare for unsafe memory access deprecated for removal (#14090) · netty/netty@21de89f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptivePoolingAllocator.java
    • test/java/io/netty/buffer
      • AlignedPooledByteBufAllocatorTest.java
      • FixedCompositeByteBufTest.java
      • PooledAlignedBigEndianDirectByteBufTest.java
      • PooledByteBufAllocatorTest.java
  • codec-http/src
    • main/java/io/netty/handler/codec/http/multipart
      • HttpPostStandardRequestDecoder.java
    • test/java/io/netty/handler/codec/http/multipart
      • HttpPostRequestDecoderTest.java
  • codec/src
    • main/java/io/netty/handler/codec/compression
      • LzfEncoder.java
    • test/java/io/netty/handler/codec
      • compression
        • LengthAwareLzfIntegrationTest.java
        • LzfDecoderTest.java
      • marshalling
        • AbstractMarshallingTest.java
  • common/src/main/java/io/netty/util/internal
    • PlatformDependent.java
    • PlatformDependent0.java
  • microbench/src/main/java/io/netty/handler/codec/http
    • HttpPostDecoderBenchmark.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ public boolean hasArray() {
705705

706706
@Override
707707
public byte[] array() {
708+
ensureAccessible();
708709
return rootParent.array();
709710
}
710711

Collapse file

‎buffer/src/test/java/io/netty/buffer/AlignedPooledByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AlignedPooledByteBufAllocatorTest.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2526

2627
public class AlignedPooledByteBufAllocatorTest extends PooledByteBufAllocatorTest {
2728
@Override
2829
protected PooledByteBufAllocator newAllocator(boolean preferDirect) {
30+
assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
2931
int directMemoryCacheAlignment = 1;
3032
return new PooledByteBufAllocator(
3133
preferDirect,
@@ -42,6 +44,7 @@ protected PooledByteBufAllocator newAllocator(boolean preferDirect) {
4244
// https://github.com/netty/netty/issues/11955
4345
@Test
4446
public void testCorrectElementSize() {
47+
assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
4548
ByteBufAllocator allocator = new PooledByteBufAllocator(
4649
true,
4750
PooledByteBufAllocator.defaultNumHeapArena(),
@@ -69,6 +72,7 @@ public void testCorrectElementSize() {
6972

7073
@Test
7174
public void testDirectSubpageReleaseLock() {
75+
assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
7276
int initialCapacity = 0;
7377
int directMemoryCacheAlignment = 32;
7478
PooledByteBufAllocator allocator = new PooledByteBufAllocator(
Collapse file

‎buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ public void testHasArrayWhenEmptyAndIsDirect() {
502502
assertTrue(buf.hasArray());
503503
assertArrayEquals(EMPTY_BUFFER.array(), buf.array());
504504
assertEquals(EMPTY_BUFFER.isDirect(), buf.isDirect());
505-
assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress());
505+
assertEquals(EMPTY_BUFFER.hasMemoryAddress(), buf.hasMemoryAddress());
506+
if (buf.hasMemoryAddress()) {
507+
assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress());
508+
}
506509
buf.release();
507510
}
508511

Collapse file

‎buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import java.nio.ByteOrder;
2222

2323
import static org.junit.jupiter.api.Assertions.assertSame;
24+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2425

2526
public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDirectByteBufTest {
2627
private static final int directMemoryCacheAlignment = 1;
2728
private static PooledByteBufAllocator allocator;
2829

2930
@BeforeAll
3031
public static void setUpAllocator() {
32+
assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
3133
allocator = new PooledByteBufAllocator(
3234
true,
3335
PooledByteBufAllocator.defaultNumHeapArena(),
Collapse file

‎buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public void testFreePoolChunk() {
277277
public void testCollapse() {
278278
int pageSize = 8192;
279279
//no cache
280-
ByteBufAllocator allocator = new PooledByteBufAllocator(true, 0, 1, 8192, 9, 0, 0, 0);
280+
ByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 9, 0, 0, 0);
281281

282282
ByteBuf b1 = allocator.buffer(pageSize * 4);
283283
ByteBuf b2 = allocator.buffer(pageSize * 5);
@@ -310,7 +310,7 @@ public void testCollapse() {
310310
@Test
311311
public void testAllocateSmallOffset() {
312312
int pageSize = 8192;
313-
ByteBufAllocator allocator = new PooledByteBufAllocator(true, 0, 1, 8192, 9, 0, 0, 0);
313+
ByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 9, 0, 0, 0);
314314

315315
int size = pageSize * 5;
316316

Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostStandardRequestDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostStandardRequestDecoder.java
+1-122Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -573,128 +573,7 @@ private void parseBodyAttributes() {
573573
if (undecodedChunk == null) {
574574
return;
575575
}
576-
if (!undecodedChunk.hasArray()) {
577-
parseBodyAttributesStandard();
578-
return;
579-
}
580-
SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk);
581-
int firstpos = undecodedChunk.readerIndex();
582-
int currentpos = firstpos;
583-
int equalpos;
584-
int ampersandpos;
585-
if (currentStatus == MultiPartStatus.NOTSTARTED) {
586-
currentStatus = MultiPartStatus.DISPOSITION;
587-
}
588-
boolean contRead = true;
589-
try {
590-
loop: while (sao.pos < sao.limit) {
591-
char read = (char) (sao.bytes[sao.pos++] & 0xFF);
592-
currentpos++;
593-
switch (currentStatus) {
594-
case DISPOSITION:// search '='
595-
if (read == '=') {
596-
currentStatus = MultiPartStatus.FIELD;
597-
equalpos = currentpos - 1;
598-
String key = decodeAttribute(undecodedChunk.toString(firstpos, equalpos - firstpos, charset),
599-
charset);
600-
currentAttribute = factory.createAttribute(request, key);
601-
firstpos = currentpos;
602-
} else if (read == '&' ||
603-
(isLastChunk && !undecodedChunk.isReadable() && hasFormBody())) { // special empty FIELD
604-
currentStatus = MultiPartStatus.DISPOSITION;
605-
ampersandpos = read == '&' ? currentpos - 1 : currentpos;
606-
String key = decodeAttribute(
607-
undecodedChunk.toString(firstpos, ampersandpos - firstpos, charset), charset);
608-
// Some weird request bodies start with an '&' char, eg: &name=J&age=17.
609-
// In that case, key would be "", will get exception:
610-
// java.lang.IllegalArgumentException: Param 'name' must not be empty;
611-
// Just check and skip empty key.
612-
if (!key.isEmpty()) {
613-
currentAttribute = factory.createAttribute(request, key);
614-
currentAttribute.setValue(""); // empty
615-
addHttpData(currentAttribute);
616-
}
617-
currentAttribute = null;
618-
firstpos = currentpos;
619-
contRead = true;
620-
}
621-
break;
622-
case FIELD:// search '&' or end of line
623-
if (read == '&') {
624-
currentStatus = MultiPartStatus.DISPOSITION;
625-
ampersandpos = currentpos - 1;
626-
setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos));
627-
firstpos = currentpos;
628-
contRead = true;
629-
} else if (read == HttpConstants.CR) {
630-
if (sao.pos < sao.limit) {
631-
read = (char) (sao.bytes[sao.pos++] & 0xFF);
632-
currentpos++;
633-
if (read == HttpConstants.LF) {
634-
currentStatus = MultiPartStatus.PREEPILOGUE;
635-
ampersandpos = currentpos - 2;
636-
sao.setReadPosition(0);
637-
setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos));
638-
firstpos = currentpos;
639-
contRead = false;
640-
break loop;
641-
} else {
642-
// Error
643-
sao.setReadPosition(0);
644-
throw new ErrorDataDecoderException("Bad end of line");
645-
}
646-
} else {
647-
if (sao.limit > 0) {
648-
currentpos--;
649-
}
650-
}
651-
} else if (read == HttpConstants.LF) {
652-
currentStatus = MultiPartStatus.PREEPILOGUE;
653-
ampersandpos = currentpos - 1;
654-
sao.setReadPosition(0);
655-
setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos));
656-
firstpos = currentpos;
657-
contRead = false;
658-
break loop;
659-
}
660-
break;
661-
default:
662-
// just stop
663-
sao.setReadPosition(0);
664-
contRead = false;
665-
break loop;
666-
}
667-
}
668-
if (isLastChunk && currentAttribute != null) {
669-
// special case
670-
ampersandpos = currentpos;
671-
if (ampersandpos > firstpos) {
672-
setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos));
673-
} else if (!currentAttribute.isCompleted()) {
674-
setFinalBuffer(Unpooled.EMPTY_BUFFER);
675-
}
676-
firstpos = currentpos;
677-
currentStatus = MultiPartStatus.EPILOGUE;
678-
} else if (contRead && currentAttribute != null && currentStatus == MultiPartStatus.FIELD) {
679-
// reset index except if to continue in case of FIELD getStatus
680-
currentAttribute.addContent(undecodedChunk.retainedSlice(firstpos, currentpos - firstpos),
681-
false);
682-
firstpos = currentpos;
683-
}
684-
undecodedChunk.readerIndex(firstpos);
685-
} catch (ErrorDataDecoderException e) {
686-
// error while decoding
687-
undecodedChunk.readerIndex(firstpos);
688-
throw e;
689-
} catch (IOException e) {
690-
// error while decoding
691-
undecodedChunk.readerIndex(firstpos);
692-
throw new ErrorDataDecoderException(e);
693-
} catch (IllegalArgumentException e) {
694-
// error while decoding
695-
undecodedChunk.readerIndex(firstpos);
696-
throw new ErrorDataDecoderException(e);
697-
}
576+
parseBodyAttributesStandard();
698577
}
699578

700579
private void setFinalBuffer(ByteBuf buffer) throws IOException {
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java
-7Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,8 @@ public void testMultipartRequest() throws Exception {
757757
assertEquals(2, decoder.getBodyHttpDatas().size());
758758

759759
Attribute attrMsg = (Attribute) decoder.getBodyHttpData("msg");
760-
assertTrue(attrMsg.getByteBuf().isDirect());
761760
assertEquals("test message", attrMsg.getValue());
762761
Attribute attrMsgId = (Attribute) decoder.getBodyHttpData("msg_id");
763-
assertTrue(attrMsgId.getByteBuf().isDirect());
764762
assertEquals("15200", attrMsgId.getValue());
765763

766764
decoder.destroy();
@@ -876,23 +874,18 @@ public void testDecodeFullHttpRequestWithUrlEncodedBody() throws Exception {
876874
assertEquals(5, decoder.getBodyHttpDatas().size());
877875

878876
Attribute attr = (Attribute) decoder.getBodyHttpData("foo");
879-
assertTrue(attr.getByteBuf().isDirect());
880877
assertEquals("bar", attr.getValue());
881878

882879
attr = (Attribute) decoder.getBodyHttpData("a");
883-
assertTrue(attr.getByteBuf().isDirect());
884880
assertEquals("b", attr.getValue());
885881

886882
attr = (Attribute) decoder.getBodyHttpData("empty");
887-
assertTrue(attr.getByteBuf().isDirect());
888883
assertEquals("", attr.getValue());
889884

890885
attr = (Attribute) decoder.getBodyHttpData("city");
891-
assertTrue(attr.getByteBuf().isDirect());
892886
assertEquals("<\"new\" york city>", attr.getValue());
893887

894888
attr = (Attribute) decoder.getBodyHttpData("other_city");
895-
assertTrue(attr.getByteBuf().isDirect());
896889
assertEquals("los angeles", attr.getValue());
897890

898891
decoder.destroy();
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/LzfEncoder.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/LzfEncoder.java
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.netty.buffer.ByteBuf;
2424
import io.netty.channel.ChannelHandlerContext;
2525
import io.netty.handler.codec.MessageToByteEncoder;
26+
import io.netty.util.internal.PlatformDependent;
2627

2728
import static com.ning.compress.lzf.LZFChunk.MAX_CHUNK_LEN;
2829

@@ -39,6 +40,7 @@ public class LzfEncoder extends MessageToByteEncoder<ByteBuf> {
3940
* less than {@link #MIN_BLOCK_TO_COMPRESS} will write as uncompressed.
4041
*/
4142
private static final int MIN_BLOCK_TO_COMPRESS = 16;
43+
private static final boolean DEFAULT_SAFE = !PlatformDependent.hasUnsafe();
4244

4345
/**
4446
* Compress threshold for LZF format. When the amount of input data is less than compressThreshold,
@@ -66,7 +68,7 @@ public class LzfEncoder extends MessageToByteEncoder<ByteBuf> {
6668
* non-standard platforms it may be necessary to use {@link #LzfEncoder(boolean)} with {@code true} param.
6769
*/
6870
public LzfEncoder() {
69-
this(false);
71+
this(DEFAULT_SAFE);
7072
}
7173

7274
/**
@@ -77,7 +79,9 @@ public LzfEncoder() {
7779
* Otherwise encoder will try to use highly optimized {@link ChunkEncoder}
7880
* implementation that uses Sun JDK's {@link sun.misc.Unsafe}
7981
* class (which may be included by other JDK's as well).
82+
* @deprecated Use the constructor without the {@code safeInstance} parameter.
8083
*/
84+
@Deprecated
8185
public LzfEncoder(boolean safeInstance) {
8286
this(safeInstance, MAX_CHUNK_LEN);
8387
}
@@ -92,7 +96,9 @@ public LzfEncoder(boolean safeInstance) {
9296
* class (which may be included by other JDK's as well).
9397
* @param totalLength Expected total length of content to compress; only matters for outgoing messages
9498
* that is smaller than maximum chunk size (64k), to optimize encoding hash tables.
99+
* @deprecated Use the constructor without the {@code safeInstance} parameter.
95100
*/
101+
@Deprecated
96102
public LzfEncoder(boolean safeInstance, int totalLength) {
97103
this(safeInstance, totalLength, MIN_BLOCK_TO_COMPRESS);
98104
}
@@ -106,7 +112,20 @@ public LzfEncoder(boolean safeInstance, int totalLength) {
106112
* to optimize encoding hash tables.
107113
*/
108114
public LzfEncoder(int totalLength) {
109-
this(false, totalLength);
115+
this(DEFAULT_SAFE, totalLength);
116+
}
117+
118+
/**
119+
* Creates a new LZF encoder with specified settings.
120+
*
121+
* @param totalLength Expected total length of content to compress; only matters for outgoing messages
122+
* that is smaller than maximum chunk size (64k), to optimize encoding hash tables.
123+
* @param compressThreshold Compress threshold for LZF format. When the amount of input data is less than
124+
* compressThreshold, we will construct an uncompressed output according
125+
* to the LZF format.
126+
*/
127+
public LzfEncoder(int totalLength, int compressThreshold) {
128+
this(DEFAULT_SAFE, totalLength, compressThreshold);
110129
}
111130

112131
/**
@@ -122,7 +141,9 @@ public LzfEncoder(int totalLength) {
122141
* @param compressThreshold Compress threshold for LZF format. When the amount of input data is less than
123142
* compressThreshold, we will construct an uncompressed output according
124143
* to the LZF format.
144+
* @deprecated Use the constructor without the {@code safeInstance} parameter.
125145
*/
146+
@Deprecated
126147
public LzfEncoder(boolean safeInstance, int totalLength, int compressThreshold) {
127148
super(false);
128149
if (totalLength < MIN_BLOCK_TO_COMPRESS || totalLength > MAX_CHUNK_LEN) {
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/LengthAwareLzfIntegrationTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/LengthAwareLzfIntegrationTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public class LengthAwareLzfIntegrationTest extends LzfIntegrationTest {
2323

2424
@Override
2525
protected EmbeddedChannel createEncoder() {
26-
return new EmbeddedChannel(new LzfEncoder(false, MAX_CHUNK_LEN, 2 * 1024 * 1024));
26+
return new EmbeddedChannel(new LzfEncoder(MAX_CHUNK_LEN, 2 * 1024 * 1024));
2727
}
2828
}
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.Unpooled;
2121
import io.netty.channel.embedded.EmbeddedChannel;
22+
import io.netty.util.internal.PlatformDependent;
2223
import org.junit.jupiter.api.Test;
2324
import org.junit.jupiter.api.function.Executable;
2425

@@ -68,6 +69,6 @@ public void execute() {
6869

6970
@Override
7071
protected byte[] compress(byte[] data) throws Exception {
71-
return LZFEncoder.encode(data);
72+
return PlatformDependent.hasUnsafe() ? LZFEncoder.encode(data) : LZFEncoder.safeEncode(data);
7273
}
7374
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.