KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > SyncByteConverter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
18  * Utility for managing slash separated sync information fields.
19  */

20 public class SyncByteConverter {
21
22     protected static final byte SEPARATOR_BYTE = (byte)'/';
23
24     /**
25      * Change the slot to the given bytes
26      * @param syncBytes the sync bytes
27      * @param slot the slot location
28      * @param newBytes the bytes to be put in the slot
29      * @return the new sync bytes
30      * @throws TeamException
31      */

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 JavaDoc[] { new String JavaDoc(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     /**
52      * Method startOfSlot returns the index of the slash that occurs before the
53      * given slot index. The provided index should be >= 1 which assumes that
54      * slot zero occurs before the first slash.
55      *
56      * @param syncBytes
57      * @param i
58      * @return int
59      */

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     /**
72      * Return the offset the the Nth delimeter from the given start index.
73      * @param bytes
74      * @param delimiter
75      * @param start
76      * @param n
77      * @return int
78      */

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         // the Nth delimeter was not found
86
return -1;
87     }
88     
89     /**
90      * Get the bytes in the given slot.
91      * @param bytes the sync bytes
92      * @param index the slot location
93      * @param includeRest whether to include the rest
94      * @return the bytes in the given slot
95      */

96     public static byte[] getSlot(byte[] bytes, int index, boolean includeRest) {
97         // Find the starting index
98
byte delimiter = SEPARATOR_BYTE;
99         int start;
100         if (index == 0) {
101             // make start -1 so that end determination will start at offset 0.
102
start = -1;
103         } else {
104             start = getOffsetOfDelimeter(bytes, delimiter, 0, index);
105             if (start == -1) return null;
106         }
107         // Find the ending index
108
int end = getOffsetOfDelimeter(bytes, delimiter, start + 1, 1);
109         // Calculate the length
110
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 JavaDoc[] slots) {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123         for (int i = 0; i < slots.length; i++) {
124             String JavaDoc string = slots[i];
125             buffer.append(string);
126             buffer.append(new String JavaDoc(new byte[] {SyncByteConverter.SEPARATOR_BYTE }));
127         }
128         return buffer.toString().getBytes();
129     }
130 }
131
Popular Tags