1 11 package org.eclipse.team.internal.core.subscribers; 12 13 import org.eclipse.osgi.util.NLS; 14 import org.eclipse.team.core.TeamException; 15 import org.eclipse.team.internal.core.Messages; 16 17 20 public class SyncByteConverter { 21 22 protected static final byte SEPARATOR_BYTE = (byte)'/'; 23 24 32 public static byte[] setSlot(byte[] syncBytes, int slot, byte[] newBytes) throws TeamException { 33 int start = startOfSlot(syncBytes, slot); 34 if (start == -1) { 35 throw new TeamException(NLS.bind(Messages.SyncByteConverter_1, new String [] { new String (syncBytes) })); 36 } 37 int end = startOfSlot(syncBytes, slot + 1); 38 int totalLength = start + 1 + newBytes.length; 39 if (end != -1) { 40 totalLength += syncBytes.length - end; 41 } 42 byte[] result = new byte[totalLength]; 43 System.arraycopy(syncBytes, 0, result, 0, start + 1); 44 System.arraycopy(newBytes, 0, result, start + 1, newBytes.length); 45 if (end != -1) { 46 System.arraycopy(syncBytes, end, result, start + 1 + newBytes.length, syncBytes.length - end); 47 } 48 return result; 49 } 50 51 60 private static int startOfSlot(byte[] syncBytes, int slot) { 61 int count = 0; 62 for (int j = 0; j < syncBytes.length; j++) { 63 if (syncBytes[j] == SEPARATOR_BYTE) { 64 count++; 65 if (count == slot) return j; 66 } 67 } 68 return -1; 69 } 70 71 79 private static int getOffsetOfDelimeter(byte[] bytes, byte delimiter, int start, int n) { 80 int count = 0; 81 for (int i = start; i < bytes.length; i++) { 82 if (bytes[i] == delimiter) count++; 83 if (count == n) return i; 84 } 85 return -1; 87 } 88 89 96 public static byte[] getSlot(byte[] bytes, int index, boolean includeRest) { 97 byte delimiter = SEPARATOR_BYTE; 99 int start; 100 if (index == 0) { 101 start = -1; 103 } else { 104 start = getOffsetOfDelimeter(bytes, delimiter, 0, index); 105 if (start == -1) return null; 106 } 107 int end = getOffsetOfDelimeter(bytes, delimiter, start + 1, 1); 109 int length; 111 if (end == -1 || includeRest) { 112 length = bytes.length - start - 1; 113 } else { 114 length = end - start - 1; 115 } 116 byte[] result = new byte[length]; 117 System.arraycopy(bytes, start + 1, result, 0, length); 118 return result; 119 } 120 121 public static byte[] toBytes(String [] slots) { 122 StringBuffer buffer = new StringBuffer (); 123 for (int i = 0; i < slots.length; i++) { 124 String string = slots[i]; 125 buffer.append(string); 126 buffer.append(new String (new byte[] {SyncByteConverter.SEPARATOR_BYTE })); 127 } 128 return buffer.toString().getBytes(); 129 } 130 } 131 | Popular Tags |