001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 12.01.2006 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: import java.util.ArrayList;
020: import java.util.Arrays;
021:
022: /**
023: * Implements some utils for array hanling.
024: *
025: * @author w.klaas
026: *
027: */
028: public final class ArrayUtils {
029:
030: /** prevent instancing. */
031: private ArrayUtils() {
032: }
033:
034: /**
035: * checking if 2 arrays are equal up to len length.
036: *
037: * @param id
038: * first array
039: * @param id2
040: * second array
041: * @param len
042: * len to check.
043: * @return <code>true</code> if the array are equal, otherwise
044: * <code>false</code>
045: */
046: public static boolean equals(final byte[] id, final byte[] id2,
047: final int len) {
048: if ((id != null) && (id2 != null)) {
049: if ((id.length >= len) && (id2.length >= len)) {
050: byte[] newid = new byte[len];
051: System.arraycopy(id, 0, newid, 0, len);
052: byte[] newid2 = new byte[len];
053: System.arraycopy(id2, 0, newid2, 0, len);
054: return Arrays.equals(newid, newid2);
055: }
056: }
057: return false;
058: }
059:
060: /**
061: * converting a Strign into an array. Format is the internal format of the
062: * toString methode of a array.
063: *
064: * @param value
065: * the string to convert.
066: * @return String[]
067: */
068: public static String[] stringToArray(final String value) {
069: String line = value.trim();
070: int pos = 0;
071: int colonCount = 0;
072: int start = 0;
073: int stop = line.length();
074: boolean inField = false;
075: ArrayList<String> list = new ArrayList<String>();
076: while (pos < line.length()) {
077: char chr = line.charAt(pos);
078: switch (chr) {
079: case '[':
080: if (!inField) {
081: colonCount++;
082: if (colonCount == 1) {
083: start = pos + 1;
084: }
085: }
086: break;
087: case ']':
088: if (!inField) {
089: colonCount--;
090: if (colonCount == 0) {
091: stop = pos;
092: list.add(line.substring(start, stop));
093: start = pos + 1;
094: }
095: }
096: break;
097: case ',':
098: if (!inField) {
099: if (colonCount == 1) {
100: stop = pos;
101: list.add(line.substring(start, stop));
102: start = pos + 1;
103: }
104: }
105: break;
106: case '"':
107: inField = !inField;
108: break;
109: default:
110: break;
111: }
112: pos++;
113: }
114: return list.toArray(new String[0]);
115: }
116: }
|