Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.nio.charset.CharacterCodingException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- public class Funktional {
- public static void main(String[] args) throws IOException {
- // byte[] input = Files.readAllBytes(Paths.get("alben.xml"));
- byte[] input = Files.readAllBytes(Paths.get("alben-a1.xml"));
- System.out.println(createTokenList(input));
- }
- public static List<String> createTokenList(CharBuffer cb, List<String> tokens, String captured, boolean onlyWhitespace) {
- if (!cb.hasRemaining()) {
- return tokens;
- }
- char c = cb.get();
- if (c == '<' || c == '>') {
- if (!onlyWhitespace) {
- tokens.add(captured);
- }
- createTokenList(cb, tokens, "", true);
- } else {
- onlyWhitespace &= c == ' ' || c == '\n' || c == '\t' || c == '\r';
- createTokenList(cb, tokens, captured + c, onlyWhitespace);
- }
- return tokens;
- }
- public static List<String> createTokenList(byte[] input) throws CharacterCodingException {
- CharBuffer cb = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(input));
- return createTokenList(cb, new ArrayList<>(), "", true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment