|
27 | 27 | import java.nio.channels.CompletionHandler;
|
28 | 28 | import java.nio.channels.ReadableByteChannel;
|
29 | 29 | import java.nio.channels.WritableByteChannel;
|
30 |
| -import java.nio.charset.Charset; |
31 | 30 | import java.nio.file.OpenOption;
|
32 | 31 | import java.nio.file.Path;
|
33 | 32 | import java.nio.file.StandardOpenOption;
|
34 |
| -import java.util.ArrayList; |
35 | 33 | import java.util.Arrays;
|
36 | 34 | import java.util.HashSet;
|
37 |
| -import java.util.List; |
38 | 35 | import java.util.Set;
|
39 | 36 | import java.util.concurrent.Callable;
|
40 | 37 | import java.util.concurrent.atomic.AtomicBoolean;
|
41 | 38 | import java.util.concurrent.atomic.AtomicLong;
|
42 | 39 | import java.util.concurrent.atomic.AtomicReference;
|
43 | 40 | import java.util.function.Consumer;
|
44 |
| -import java.util.function.IntPredicate; |
45 | 41 |
|
46 | 42 | import org.reactivestreams.Publisher;
|
47 | 43 | import org.reactivestreams.Subscription;
|
@@ -572,147 +568,6 @@ public static Matcher matcher(byte[]... delimiters) {
|
572 | 568 | }
|
573 | 569 | }
|
574 | 570 |
|
575 |
| - /** |
576 |
| - * Splits the given stream of data buffers around the given delimiter. |
577 |
| - * The returned flux contains data buffers that are terminated by the given delimiter, |
578 |
| - * though the delimiter itself is removed. |
579 |
| - * @param dataBuffers the input stream of data buffers |
580 |
| - * @param delimiter the delimiting byte array |
581 |
| - * @return the flux of data buffers created by splitting the given data buffers around the |
582 |
| - * given delimiter |
583 |
| - * @since 5.2 |
584 |
| - */ |
585 |
| - public static Flux<DataBuffer> split(Publisher<DataBuffer> dataBuffers, byte[] delimiter) { |
586 |
| - return split(dataBuffers, delimiter, true); |
587 |
| - } |
588 |
| - |
589 |
| - /** |
590 |
| - * Splits the given stream of data buffers around the given delimiter. |
591 |
| - * The returned flux contains data buffers that are terminated by the given delimiter, |
592 |
| - * which is included when {@code stripDelimiter} is {@code false}. |
593 |
| - * @param dataBuffers the input stream of data buffers |
594 |
| - * @param delimiter the delimiter bytes |
595 |
| - * @param stripDelimiter whether to include the delimiter at the end of each resulting buffer |
596 |
| - * @return the flux of data buffers created by splitting the given data buffers around the |
597 |
| - * given delimiter |
598 |
| - * @since 5.2 |
599 |
| - */ |
600 |
| - public static Flux<DataBuffer> split(Publisher<DataBuffer> dataBuffers, byte[] delimiter, |
601 |
| - boolean stripDelimiter) { |
602 |
| - |
603 |
| - return split(dataBuffers, new byte[][]{delimiter}, stripDelimiter); |
604 |
| - } |
605 |
| - |
606 |
| - /** |
607 |
| - * Splits the given stream of data buffers around the given delimiters. |
608 |
| - * The returned flux contains data buffers that are terminated by any of the given delimiters, |
609 |
| - * which are included when {@code stripDelimiter} is {@code false}. |
610 |
| - * @param dataBuffers the input stream of data buffers |
611 |
| - * @param delimiters the delimiters, one per element |
612 |
| - * @param stripDelimiter whether to include the delimiters at the end of each resulting buffer |
613 |
| - * @return the flux of data buffers created by splitting the given data buffers around the |
614 |
| - * given delimiters |
615 |
| - * @since 5.2 |
616 |
| - */ |
617 |
| - public static Flux<DataBuffer> split(Publisher<DataBuffer> dataBuffers, byte[][] delimiters, |
618 |
| - boolean stripDelimiter) { |
619 |
| - Assert.notNull(dataBuffers, "DataBuffers must not be null"); |
620 |
| - Assert.isTrue(delimiters.length > 0, "Delimiter must not be empty"); |
621 |
| - |
622 |
| - Matcher[] matchers = matchers(delimiters); |
623 |
| - |
624 |
| - return Flux.from(dataBuffers) |
625 |
| - .flatMap(buffer -> endFrameAfterDelimiter(buffer, matchers)) |
626 |
| - .bufferUntil(buffer -> buffer instanceof EndFrameBuffer) |
627 |
| - .map(buffers -> joinAndStrip(buffers, stripDelimiter)) |
628 |
| - .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release); |
629 |
| - } |
630 |
| - |
631 |
| - private static Matcher[] matchers(byte[][] delimiters) { |
632 |
| - Assert.isTrue(delimiters.length > 0, "Delimiters must not be empty"); |
633 |
| - Matcher[] result = new Matcher[delimiters.length]; |
634 |
| - for (int i = 0; i < delimiters.length; i++) { |
635 |
| - result[i] = matcher(delimiters[i]); |
636 |
| - } |
637 |
| - return result; |
638 |
| - } |
639 |
| - |
640 |
| - /** |
641 |
| - * Finds the {@link Matcher} with the first match and longest delimiter, and inserts a |
642 |
| - * {@link EndFrameBuffer} just after its match. |
643 |
| - * |
644 |
| - * @param dataBuffer the buffer to find delimiters in |
645 |
| - * @param matchers used to find the first delimiters |
646 |
| - * @return a flux of buffers, containing {@link EndFrameBuffer} after each delimiter that was |
647 |
| - * found in {@code dataBuffer}. Returns Flux, because returning List (w/ flatMapIterable) |
648 |
| - * results in memory leaks due to pre-fetching. |
649 |
| - */ |
650 |
| - private static Flux<DataBuffer> endFrameAfterDelimiter(DataBuffer dataBuffer, Matcher[] matchers) { |
651 |
| - List<DataBuffer> result = new ArrayList<>(); |
652 |
| - do { |
653 |
| - int matchedEndIdx = Integer.MAX_VALUE; |
654 |
| - byte[] matchedDelimiter = new byte[0]; |
655 |
| - for (Matcher matcher : matchers) { |
656 |
| - int endIdx = matcher.match(dataBuffer); |
657 |
| - if (endIdx != -1 && |
658 |
| - endIdx <= matchedEndIdx && |
659 |
| - matcher.delimiter().length > matchedDelimiter.length) { |
660 |
| - matchedEndIdx = endIdx; |
661 |
| - matchedDelimiter = matcher.delimiter(); |
662 |
| - } |
663 |
| - } |
664 |
| - if (matchedDelimiter.length > 0) { |
665 |
| - int readPosition = dataBuffer.readPosition(); |
666 |
| - int length = matchedEndIdx + 1 - readPosition ; |
667 |
| - result.add(dataBuffer.retainedSlice(readPosition, length)); |
668 |
| - result.add(new EndFrameBuffer(matchedDelimiter)); |
669 |
| - dataBuffer.readPosition(matchedEndIdx + 1); |
670 |
| - |
671 |
| - for (Matcher matcher : matchers) { |
672 |
| - matcher.reset(); |
673 |
| - } |
674 |
| - } |
675 |
| - else { |
676 |
| - result.add(retain(dataBuffer)); |
677 |
| - break; |
678 |
| - } |
679 |
| - } |
680 |
| - while (dataBuffer.readableByteCount() > 0); |
681 |
| - |
682 |
| - DataBufferUtils.release(dataBuffer); |
683 |
| - return Flux.fromIterable(result); |
684 |
| - } |
685 |
| - |
686 |
| - /** |
687 |
| - * Joins the given list of buffers. If the list ends with a {@link EndFrameBuffer}, it is |
688 |
| - * removed. If {@code stripDelimiter} is {@code true} and the resulting buffer ends with |
689 |
| - * a delimiter, it is removed. |
690 |
| - * @param dataBuffers the data buffers to join |
691 |
| - * @param stripDelimiter whether to strip the delimiter |
692 |
| - * @return the joined buffer |
693 |
| - */ |
694 |
| - private static DataBuffer joinAndStrip(List<DataBuffer> dataBuffers, |
695 |
| - boolean stripDelimiter) { |
696 |
| - |
697 |
| - Assert.state(!dataBuffers.isEmpty(), "DataBuffers should not be empty"); |
698 |
| - |
699 |
| - byte[] matchingDelimiter = null; |
700 |
| - |
701 |
| - int lastIdx = dataBuffers.size() - 1; |
702 |
| - DataBuffer lastBuffer = dataBuffers.get(lastIdx); |
703 |
| - if (lastBuffer instanceof EndFrameBuffer) { |
704 |
| - matchingDelimiter = ((EndFrameBuffer) lastBuffer).delimiter(); |
705 |
| - dataBuffers.remove(lastIdx); |
706 |
| - } |
707 |
| - |
708 |
| - DataBuffer result = dataBuffers.get(0).factory().join(dataBuffers); |
709 |
| - |
710 |
| - if (stripDelimiter && matchingDelimiter != null) { |
711 |
| - result.writePosition(result.writePosition() - matchingDelimiter.length); |
712 |
| - } |
713 |
| - return result; |
714 |
| - } |
715 |
| - |
716 | 571 |
|
717 | 572 | /**
|
718 | 573 | * Defines an object that matches a data buffer against a delimiter.
|
@@ -1107,167 +962,4 @@ public void reset() {
|
1107 | 962 | }
|
1108 | 963 | }
|
1109 | 964 |
|
1110 |
| - |
1111 |
| - |
1112 |
| - private static class EndFrameBuffer implements DataBuffer { |
1113 |
| - |
1114 |
| - private static final DataBuffer BUFFER = new DefaultDataBufferFactory().wrap(new byte[0]); |
1115 |
| - |
1116 |
| - private byte[] delimiter; |
1117 |
| - |
1118 |
| - |
1119 |
| - public EndFrameBuffer(byte[] delimiter) { |
1120 |
| - this.delimiter = delimiter; |
1121 |
| - } |
1122 |
| - |
1123 |
| - public byte[] delimiter() { |
1124 |
| - return this.delimiter; |
1125 |
| - } |
1126 |
| - |
1127 |
| - @Override |
1128 |
| - public DataBufferFactory factory() { |
1129 |
| - return BUFFER.factory(); |
1130 |
| - } |
1131 |
| - |
1132 |
| - @Override |
1133 |
| - public int indexOf(IntPredicate predicate, int fromIndex) { |
1134 |
| - return BUFFER.indexOf(predicate, fromIndex); |
1135 |
| - } |
1136 |
| - |
1137 |
| - @Override |
1138 |
| - public int lastIndexOf(IntPredicate predicate, int fromIndex) { |
1139 |
| - return BUFFER.lastIndexOf(predicate, fromIndex); |
1140 |
| - } |
1141 |
| - |
1142 |
| - @Override |
1143 |
| - public int readableByteCount() { |
1144 |
| - return BUFFER.readableByteCount(); |
1145 |
| - } |
1146 |
| - |
1147 |
| - @Override |
1148 |
| - public int writableByteCount() { |
1149 |
| - return BUFFER.writableByteCount(); |
1150 |
| - } |
1151 |
| - |
1152 |
| - @Override |
1153 |
| - public int capacity() { |
1154 |
| - return BUFFER.capacity(); |
1155 |
| - } |
1156 |
| - |
1157 |
| - @Override |
1158 |
| - public DataBuffer capacity(int capacity) { |
1159 |
| - return BUFFER.capacity(capacity); |
1160 |
| - } |
1161 |
| - |
1162 |
| - @Override |
1163 |
| - public DataBuffer ensureCapacity(int capacity) { |
1164 |
| - return BUFFER.ensureCapacity(capacity); |
1165 |
| - } |
1166 |
| - |
1167 |
| - @Override |
1168 |
| - public int readPosition() { |
1169 |
| - return BUFFER.readPosition(); |
1170 |
| - } |
1171 |
| - |
1172 |
| - @Override |
1173 |
| - public DataBuffer readPosition(int readPosition) { |
1174 |
| - return BUFFER.readPosition(readPosition); |
1175 |
| - } |
1176 |
| - |
1177 |
| - @Override |
1178 |
| - public int writePosition() { |
1179 |
| - return BUFFER.writePosition(); |
1180 |
| - } |
1181 |
| - |
1182 |
| - @Override |
1183 |
| - public DataBuffer writePosition(int writePosition) { |
1184 |
| - return BUFFER.writePosition(writePosition); |
1185 |
| - } |
1186 |
| - |
1187 |
| - @Override |
1188 |
| - public byte getByte(int index) { |
1189 |
| - return BUFFER.getByte(index); |
1190 |
| - } |
1191 |
| - |
1192 |
| - @Override |
1193 |
| - public byte read() { |
1194 |
| - return BUFFER.read(); |
1195 |
| - } |
1196 |
| - |
1197 |
| - @Override |
1198 |
| - public DataBuffer read(byte[] destination) { |
1199 |
| - return BUFFER.read(destination); |
1200 |
| - } |
1201 |
| - |
1202 |
| - @Override |
1203 |
| - public DataBuffer read(byte[] destination, int offset, int length) { |
1204 |
| - return BUFFER.read(destination, offset, length); |
1205 |
| - } |
1206 |
| - |
1207 |
| - @Override |
1208 |
| - public DataBuffer write(byte b) { |
1209 |
| - return BUFFER.write(b); |
1210 |
| - } |
1211 |
| - |
1212 |
| - @Override |
1213 |
| - public DataBuffer write(byte[] source) { |
1214 |
| - return BUFFER.write(source); |
1215 |
| - } |
1216 |
| - |
1217 |
| - @Override |
1218 |
| - public DataBuffer write(byte[] source, int offset, int length) { |
1219 |
| - return BUFFER.write(source, offset, length); |
1220 |
| - } |
1221 |
| - |
1222 |
| - @Override |
1223 |
| - public DataBuffer write(DataBuffer... buffers) { |
1224 |
| - return BUFFER.write(buffers); |
1225 |
| - } |
1226 |
| - |
1227 |
| - @Override |
1228 |
| - public DataBuffer write(ByteBuffer... buffers) { |
1229 |
| - return BUFFER.write(buffers); |
1230 |
| - } |
1231 |
| - |
1232 |
| - @Override |
1233 |
| - public DataBuffer write(CharSequence charSequence, Charset charset) { |
1234 |
| - return BUFFER.write(charSequence, charset); |
1235 |
| - } |
1236 |
| - |
1237 |
| - @Override |
1238 |
| - public DataBuffer slice(int index, int length) { |
1239 |
| - return BUFFER.slice(index, length); |
1240 |
| - } |
1241 |
| - |
1242 |
| - @Override |
1243 |
| - public DataBuffer retainedSlice(int index, int length) { |
1244 |
| - return BUFFER.retainedSlice(index, length); |
1245 |
| - } |
1246 |
| - |
1247 |
| - @Override |
1248 |
| - public ByteBuffer asByteBuffer() { |
1249 |
| - return BUFFER.asByteBuffer(); |
1250 |
| - } |
1251 |
| - |
1252 |
| - @Override |
1253 |
| - public ByteBuffer asByteBuffer(int index, int length) { |
1254 |
| - return BUFFER.asByteBuffer(index, length); |
1255 |
| - } |
1256 |
| - |
1257 |
| - @Override |
1258 |
| - public InputStream asInputStream() { |
1259 |
| - return BUFFER.asInputStream(); |
1260 |
| - } |
1261 |
| - |
1262 |
| - @Override |
1263 |
| - public InputStream asInputStream(boolean releaseOnClose) { |
1264 |
| - return BUFFER.asInputStream(releaseOnClose); |
1265 |
| - } |
1266 |
| - |
1267 |
| - @Override |
1268 |
| - public OutputStream asOutputStream() { |
1269 |
| - return BUFFER.asOutputStream(); |
1270 |
| - } |
1271 |
| - } |
1272 |
| - |
1273 | 965 | }
|
0 commit comments