Here you can find the source of splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
Parameter | Description |
---|---|
theString | the <tt>String</tt> to be tokenized. |
separatorByteSequence | the byte sequence separator between tokens. |
public static List<String> splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009, 2016 GreenVulcano ESB Open Source Project. * All rights reserved.//from w w w . j a v a 2 s. com * * This file is part of GreenVulcano ESB. * * GreenVulcano ESB is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GreenVulcano ESB is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with GreenVulcano ESB. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { /** * This method tokenizes a given string using a given byte sequence as * separator and returns a <tt>List</tt> containing found tokens. The * returned <tt>List</tt> is NEVER null (it may have zero components, * anyway). * * @param theString * the <tt>String</tt> to be tokenized. * @param separatorByteSequence * the byte sequence separator between tokens. * @return a <tt>List</tt> containing found tokens. */ public static List<String> splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence) { List<String> tokenList = new ArrayList<String>(); if ((theString == null) || theString.equals("")) { return tokenList; } byte[] input = theString.getBytes(); if (Arrays.equals(input, separatorByteSequence)) { return tokenList; } if ((separatorByteSequence == null) || (separatorByteSequence.length == 0)) { tokenList.add(theString); return tokenList; } if (findByteSequenceWithinByteArray(input, separatorByteSequence) == -1) { tokenList.add(theString); return tokenList; } int startNextToken = 0; int endNextToken = 0; int maxPosition = input.length; while (startNextToken < maxPosition) { endNextToken = findByteSequenceWithinByteArray(input, separatorByteSequence, startNextToken); if (endNextToken != -1) { if (endNextToken > startNextToken) { byte[] buffer = new byte[endNextToken - startNextToken]; System.arraycopy(input, startNextToken, buffer, 0, buffer.length); String currToken = new String(buffer); tokenList.add(currToken); } startNextToken = endNextToken + separatorByteSequence.length; } else { if (startNextToken != (maxPosition - 1)) { byte[] buffer = new byte[maxPosition - startNextToken]; System.arraycopy(input, startNextToken, buffer, 0, buffer.length); String currToken = new String(buffer); tokenList.add(currToken); startNextToken = maxPosition; } } } return tokenList; } /** * Returns the index of the first occurrence of the given byte sequence * within a given byte array starting at the specified index, or -1 if the * byte sequence does not occur. * * @param input * A given byte array. * @param byteSequence * A given byte sequence. * @param fromIndex * The index to start the search from. * @return the index of the first occurrence of the given byte sequence * within a given byte array starting at the specified index, or -1 * if the byte sequence does not occur. */ private static int findByteSequenceWithinByteArray(byte[] input, byte[] byteSequence, int fromIndex) { int result = -1; if ((input == null) || (byteSequence == null)) { return result; } if ((fromIndex < 0) || (fromIndex > (input.length - 1))) { return result; } if ((input.length - fromIndex) < byteSequence.length) { return result; } byte[] buffer = new byte[byteSequence.length]; for (int i = fromIndex; i <= (input.length - byteSequence.length); i++) { System.arraycopy(input, i, buffer, 0, buffer.length); if (Arrays.equals(buffer, byteSequence)) { result = i; break; } } return result; } /** * Returns the index of the first occurrence of the given byte sequence into * a byte array obtained from the given string, or -1 if the byte sequence * does not occur. * * @param input * A given string. * @param byteSequence * A given byte sequence. * @return the index of the first occurrence of the given byte sequence into * a byte array obtained from the given string, or -1 if the byte * sequence does not occur. * */ private static int findByteSequenceWithinByteArray(byte[] input, byte[] byteSequence) { return findByteSequenceWithinByteArray(input, byteSequence, 0); } }