blob: 5fdd3276a742c4ac7d25fbd4e300d156616b2174 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Jonathan Hartdec62d42014-09-22 15:59:04 -070016package org.onlab.packet;
17
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070018import java.net.InetAddress;
19import java.net.Inet4Address;
20import java.net.Inet6Address;
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070021import java.nio.ByteBuffer;
Jonathan Hartdec62d42014-09-22 15:59:04 -070022import java.util.Arrays;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070023import java.util.Objects;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070024import com.google.common.net.InetAddresses;
25import com.google.common.primitives.UnsignedBytes;
26
Jonathan Hartdec62d42014-09-22 15:59:04 -070027
28/**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070029 * A class representing an IP address.
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080030 * This class is immutable.
Jonathan Hartdec62d42014-09-22 15:59:04 -070031 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080032public class IpAddress implements Comparable<IpAddress> {
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -070033 private static final int BIT_MASK = 0x000000ff;
34
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070035 // IP Versions
Jonathan Hartdec62d42014-09-22 15:59:04 -070036 public enum Version { INET, INET6 };
37
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070038 // lengths of address, in bytes
39 public static final int INET_BYTE_LENGTH = 4;
40 public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE;
41 public static final int INET6_BYTE_LENGTH = 16;
42 public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE;
Jonathan Hartdec62d42014-09-22 15:59:04 -070043
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070044 private final Version version;
45 private final byte[] octets;
Jonathan Hartdec62d42014-09-22 15:59:04 -070046
47 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070048 * Constructor for given IP address version and address octets.
49 *
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070050 * @param version the IP address version
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070051 * @param value the IP address value stored in network byte order
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070052 * (i.e., the most significant byte first)
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070053 * @throws IllegalArgumentException if the arguments are invalid
Jonathan Hartdec62d42014-09-22 15:59:04 -070054 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080055 protected IpAddress(Version version, byte[] value) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070056 checkArguments(version, value, 0);
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070057 this.version = version;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070058 switch (version) {
59 case INET:
60 this.octets = Arrays.copyOf(value, INET_BYTE_LENGTH);
61 break;
62 case INET6:
63 this.octets = Arrays.copyOf(value, INET6_BYTE_LENGTH);
64 break;
65 default:
66 // Should not be reached
67 this.octets = null;
68 break;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070069 }
Jonathan Hartdec62d42014-09-22 15:59:04 -070070 }
71
72 /**
73 * Returns the IP version of this address.
74 *
75 * @return the version
76 */
77 public Version version() {
78 return this.version;
79 }
80
81 /**
Pavlin Radoslavov34ffe722015-03-10 12:48:55 -070082 * Tests whether the IP version of this address is IPv4.
83 *
84 * @return true if the IP version of this address is IPv4, otherwise false.
85 */
86 public boolean isIp4() {
87 return (version() == Ip4Address.VERSION);
88 }
89
90 /**
91 * Tests whether the IP version of this address is IPv6.
92 *
93 * @return true if the IP version of this address is IPv6, otherwise false.
94 */
95 public boolean isIp6() {
96 return (version() == Ip6Address.VERSION);
97 }
98
99 /**
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800100 * Gets the {@link Ip4Address} view of the IP address.
101 *
102 * @return the {@link Ip4Address} view of the IP address if it is IPv4,
103 * otherwise null
104 */
105 public Ip4Address getIp4Address() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700106 if (!isIp4()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800107 return null;
108 }
109
110 // Return this object itself if it is already instance of Ip4Address
111 if (this instanceof Ip4Address) {
112 return (Ip4Address) this;
113 }
114 return Ip4Address.valueOf(octets);
115 }
116
117 /**
118 * Gets the {@link Ip6Address} view of the IP address.
119 *
120 * @return the {@link Ip6Address} view of the IP address if it is IPv6,
121 * otherwise null
122 */
123 public Ip6Address getIp6Address() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700124 if (!isIp6()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800125 return null;
126 }
127
128 // Return this object itself if it is already instance of Ip6Address
129 if (this instanceof Ip6Address) {
130 return (Ip6Address) this;
131 }
132 return Ip6Address.valueOf(octets);
133 }
134
135 /**
Jonathan Hartdec62d42014-09-22 15:59:04 -0700136 * Returns the IP address as a byte array.
137 *
138 * @return a byte array
139 */
140 public byte[] toOctets() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700141 return Arrays.copyOf(octets, octets.length);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700142 }
143
144 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700145 * Computes the IP address byte length for a given IP version.
146 *
147 * @param version the IP version
148 * @return the IP address byte length for the IP version
149 * @throws IllegalArgumentException if the IP version is invalid
150 */
151 public static int byteLength(Version version) {
152 switch (version) {
153 case INET:
154 return INET_BYTE_LENGTH;
155 case INET6:
156 return INET6_BYTE_LENGTH;
157 default:
158 String msg = "Invalid IP version " + version;
159 throw new IllegalArgumentException(msg);
160 }
161 }
162
163 /**
164 * Converts an integer into an IPv4 address.
165 *
166 * @param value an integer representing an IPv4 address value
167 * @return an IP address
168 */
169 public static IpAddress valueOf(int value) {
170 byte[] bytes =
171 ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array();
172 return new IpAddress(Version.INET, bytes);
173 }
174
175 /**
176 * Converts a byte array into an IP address.
177 *
178 * @param version the IP address version
179 * @param value the IP address value stored in network byte order
180 * (i.e., the most significant byte first)
181 * @return an IP address
182 * @throws IllegalArgumentException if the arguments are invalid
183 */
184 public static IpAddress valueOf(Version version, byte[] value) {
185 return new IpAddress(version, value);
186 }
187
188 /**
189 * Converts a byte array and a given offset from the beginning of the
190 * array into an IP address.
191 * <p>
192 * The IP address is stored in network byte order (i.e., the most
193 * significant byte first).
194 * </p>
195 * @param version the IP address version
196 * @param value the value to use
197 * @param offset the offset in bytes from the beginning of the byte array
198 * @return an IP address
199 * @throws IllegalArgumentException if the arguments are invalid
200 */
201 public static IpAddress valueOf(Version version, byte[] value,
202 int offset) {
203 checkArguments(version, value, offset);
204 byte[] bc = Arrays.copyOfRange(value, offset, value.length);
205 return IpAddress.valueOf(version, bc);
206 }
207
208 /**
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700209 * Converts an InetAddress into an IP address.
210 *
211 * @param inetAddress the InetAddress value to use
212 * @return an IP address
213 * @throws IllegalArgumentException if the argument is invalid
214 */
215 public static IpAddress valueOf(InetAddress inetAddress) {
216 byte[] bytes = inetAddress.getAddress();
217 if (inetAddress instanceof Inet4Address) {
218 return new IpAddress(Version.INET, bytes);
219 }
220 if (inetAddress instanceof Inet6Address) {
221 return new IpAddress(Version.INET6, bytes);
222 }
223 // Use the number of bytes as a hint
224 if (bytes.length == INET_BYTE_LENGTH) {
225 return new IpAddress(Version.INET, bytes);
226 }
227 if (bytes.length == INET6_BYTE_LENGTH) {
228 return new IpAddress(Version.INET6, bytes);
229 }
230 final String msg = "Unrecognized IP version address string: " +
231 inetAddress.toString();
232 throw new IllegalArgumentException(msg);
233 }
234
235 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700236 * Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or
237 * "1111:2222::8888") into an IP address.
238 *
239 * @param value an IP address value in string form
240 * @return an IP address
241 * @throws IllegalArgumentException if the argument is invalid
242 */
243 public static IpAddress valueOf(String value) {
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700244 InetAddress inetAddress = null;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700245 try {
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700246 inetAddress = InetAddresses.forString(value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700247 } catch (IllegalArgumentException e) {
248 final String msg = "Invalid IP address string: " + value;
249 throw new IllegalArgumentException(msg);
250 }
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700251 return valueOf(inetAddress);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700252 }
253
254 /**
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700255 * Creates an IP network mask prefix.
256 *
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700257 * @param version the IP address version
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700258 * @param prefixLength the length of the mask prefix. Must be in the
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700259 * interval [0, 32] for IPv4, or [0, 128] for IPv6
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700260 * @return a new IP address that contains a mask prefix of the
261 * specified length
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700262 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700263 */
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700264 public static IpAddress makeMaskPrefix(Version version, int prefixLength) {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800265 byte[] mask = makeMaskPrefixArray(version, prefixLength);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700266 return new IpAddress(version, mask);
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700267 }
268
269 /**
270 * Creates an IP address by masking it with a network mask of given
271 * mask length.
272 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800273 * @param address the address to mask
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700274 * @param prefixLength the length of the mask prefix. Must be in the
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700275 * interval [0, 32] for IPv4, or [0, 128] for IPv6
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700276 * @return a new IP address that is masked with a mask prefix of the
277 * specified length
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700278 * @throws IllegalArgumentException if the prefix length is invalid
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700279 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800280 public static IpAddress makeMaskedAddress(final IpAddress address,
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700281 int prefixLength) {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800282 if (address instanceof Ip4Address) {
283 Ip4Address ip4a = (Ip4Address) address;
284 return Ip4Address.makeMaskedAddress(ip4a, prefixLength);
285 } else if (address instanceof Ip6Address) {
286 Ip6Address ip6a = (Ip6Address) address;
287 return Ip6Address.makeMaskedAddress(ip6a, prefixLength);
288 } else {
289 byte[] net = makeMaskedAddressArray(address, prefixLength);
290 return IpAddress.valueOf(address.version(), net);
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700291 }
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700292 }
293
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800294 /**
295 * Check if this IP address is zero.
296 *
Thomas Vachuskae7966102015-09-09 17:33:33 -0700297 * @return true if this address is zero
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800298 */
299 public boolean isZero() {
300 for (byte b : octets) {
301 if (b != 0) {
302 return false;
303 }
304 }
305 return true;
306 }
307
Thomas Vachuskae7966102015-09-09 17:33:33 -0700308 /**
309 * Check if this IP address is self-assigned.
310 *
311 * @return true if this address is self-assigned
312 */
313 public boolean isSelfAssigned() {
314 return isIp4() && octets[0] == (byte) 169;
315 }
316
Jonathan Hartdec62d42014-09-22 15:59:04 -0700317 @Override
Jonathan Hartab63aac2014-10-16 08:52:55 -0700318 public int compareTo(IpAddress o) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700319 // Compare first the version
320 if (this.version != o.version) {
321 return this.version.compareTo(o.version);
322 }
323
324 // Compare the bytes, one-by-one
325 for (int i = 0; i < this.octets.length; i++) {
326 if (this.octets[i] != o.octets[i]) {
327 return UnsignedBytes.compare(this.octets[i], o.octets[i]);
328 }
329 }
330 return 0; // Equal
Jonathan Hartab63aac2014-10-16 08:52:55 -0700331 }
332
333 @Override
Jonathan Hartdec62d42014-09-22 15:59:04 -0700334 public int hashCode() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700335 return Objects.hash(version, Arrays.hashCode(octets));
Jonathan Hartdec62d42014-09-22 15:59:04 -0700336 }
337
338 @Override
339 public boolean equals(Object obj) {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700340 if (this == obj) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700341 return true;
342 }
Pavlin Radoslavov50b70672014-11-05 11:22:25 -0800343 if ((obj == null) || (!(obj instanceof IpAddress))) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700344 return false;
345 }
346 IpAddress other = (IpAddress) obj;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700347 return (version == other.version) &&
348 Arrays.equals(octets, other.octets);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700349 }
350
351 @Override
352 /*
353 * (non-Javadoc)
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700354 * The string representation of the IP address: "x.x.x.x" for IPv4
355 * addresses, or ':' separated string for IPv6 addresses.
Jonathan Hartdec62d42014-09-22 15:59:04 -0700356 *
357 * @see java.lang.Object#toString()
358 */
359 public String toString() {
Brian O'Connor041515f2015-02-19 15:36:17 -0800360 // FIXME InetAddress is super slow
361 switch (version) {
362 case INET:
363 return String.format("%d.%d.%d.%d", octets[0] & 0xff,
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700364 octets[1] & 0xff,
365 octets[2] & 0xff,
366 octets[3] & 0xff);
Brian O'Connor041515f2015-02-19 15:36:17 -0800367 case INET6:
368 default:
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700369 return ipv6ToStringHelper();
Jonathan Hartdec62d42014-09-22 15:59:04 -0700370 }
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700371 }
372
373 /**
Pingping Line28ae4c2015-03-13 11:37:03 -0700374 * Generates an IP prefix.
375 *
376 * @return the IP prefix of the IP address
377 */
378 public IpPrefix toIpPrefix() {
379
380 if (isIp4()) {
381 return IpPrefix.valueOf(new IpAddress(Version.INET, octets),
382 Ip4Address.BIT_LENGTH);
383 } else {
384 return IpPrefix.valueOf(new IpAddress(Version.INET6, octets),
385 Ip6Address.BIT_LENGTH);
386 }
387 }
388
389 /**
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700390 * Gets the IP address name for the IP address version.
391 *
392 * @param version the IP address version
393 * @return the IP address name for the IP address version
394 */
395 private static String addressName(Version version) {
396 switch (version) {
397 case INET:
398 return "IPv4";
399 case INET6:
400 return "IPv6";
401 default:
402 break;
403 }
404 return "UnknownIP(" + version + ")";
405 }
406
407 /**
408 * Checks whether the arguments are valid.
409 *
410 * @param version the IP address version
411 * @param value the IP address value stored in a byte array
412 * @param offset the offset in bytes from the beginning of the byte
413 * array with the address
414 * @throws IllegalArgumentException if any of the arguments is invalid
415 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800416 static void checkArguments(Version version, byte[] value, int offset) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700417 // Check the offset and byte array length
418 int addrByteLength = byteLength(version);
419 if ((offset < 0) || (offset + addrByteLength > value.length)) {
420 String msg;
421 if (value.length < addrByteLength) {
422 msg = "Invalid " + addressName(version) +
423 " address array: array length: " + value.length +
424 ". Must be at least " + addrByteLength;
425 } else {
426 msg = "Invalid " + addressName(version) +
427 " address array: array offset: " + offset +
428 ". Must be in the interval [0, " +
429 (value.length - addrByteLength) + "]";
430 }
431 throw new IllegalArgumentException(msg);
432 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700433 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800434
435 /**
436 * Creates a byte array for IP network mask prefix.
437 *
438 * @param version the IP address version
439 * @param prefixLength the length of the mask prefix. Must be in the
440 * interval [0, 32] for IPv4, or [0, 128] for IPv6
441 * @return a byte array that contains a mask prefix of the
442 * specified length
443 * @throws IllegalArgumentException if the arguments are invalid
444 */
445 static byte[] makeMaskPrefixArray(Version version, int prefixLength) {
446 int addrByteLength = byteLength(version);
447 int addrBitLength = addrByteLength * Byte.SIZE;
448
449 // Verify the prefix length
450 if ((prefixLength < 0) || (prefixLength > addrBitLength)) {
451 final String msg = "Invalid IP prefix length: " + prefixLength +
452 ". Must be in the interval [0, " + addrBitLength + "].";
453 throw new IllegalArgumentException(msg);
454 }
455
456 // Number of bytes and extra bits that should be all 1s
457 int maskBytes = prefixLength / Byte.SIZE;
458 int maskBits = prefixLength % Byte.SIZE;
459 byte[] mask = new byte[addrByteLength];
460
461 // Set the bytes and extra bits to 1s
462 for (int i = 0; i < maskBytes; i++) {
463 mask[i] = (byte) 0xff; // Set mask bytes to 1s
464 }
465 for (int i = maskBytes; i < addrByteLength; i++) {
466 mask[i] = 0; // Set remaining bytes to 0s
467 }
468 if (maskBits > 0) {
469 mask[maskBytes] = (byte) (0xff << (Byte.SIZE - maskBits));
470 }
471 return mask;
472 }
473
474 /**
475 * Creates a byte array that represents an IP address masked with
476 * a network mask of given mask length.
477 *
478 * @param addr the address to mask
479 * @param prefixLength the length of the mask prefix. Must be in the
480 * interval [0, 32] for IPv4, or [0, 128] for IPv6
481 * @return a byte array that represents the IP address masked with
482 * a mask prefix of the specified length
483 * @throws IllegalArgumentException if the prefix length is invalid
484 */
485 static byte[] makeMaskedAddressArray(final IpAddress addr,
486 int prefixLength) {
487 byte[] mask = IpAddress.makeMaskPrefixArray(addr.version(),
488 prefixLength);
489 byte[] net = new byte[mask.length];
490
491 // Mask each byte
492 for (int i = 0; i < net.length; i++) {
493 net[i] = (byte) (addr.octets[i] & mask[i]);
494 }
495 return net;
496 }
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700497
498 /**
499 * Creates a string based on the IPv6 recommendations for canonical representations found here:
500 * https://tools.ietf.org/html/rfc5952#section-1.
501 * @return A properly formatted IPv6 canonical representation.
502 */
503 private String ipv6ToStringHelper() {
504 //Populate a buffer with the string of the full address with leading zeros stripped
505 StringBuffer buff = new StringBuffer();
506 buff.append(String.format("%x:%x:%x:%x:%x:%x:%x:%x",
507 (((octets[0] & BIT_MASK) << 8) | (octets[1] & BIT_MASK)),
508 (((octets[2] & BIT_MASK) << 8) | (octets[3] & BIT_MASK)),
509 (((octets[4] & BIT_MASK) << 8) | (octets[5] & BIT_MASK)),
510 (((octets[6] & BIT_MASK) << 8) | (octets[7] & BIT_MASK)),
511 (((octets[8] & BIT_MASK) << 8) | (octets[9] & BIT_MASK)),
512 (((octets[10] & BIT_MASK) << 8) | (octets[11] & BIT_MASK)),
513 (((octets[12] & BIT_MASK) << 8) | (octets[13] & BIT_MASK)),
514 (((octets[14] & BIT_MASK) << 8) | (octets[15] & BIT_MASK))));
515 //Initialize variables for tracking longest zero subsequence, tiebreaking by first occurence
516 int longestSeqStart, longestSeqLen, currSeqStart, currSeqLen;
517 longestSeqStart = 0;
518 longestSeqLen = 0;
519 currSeqStart = 0;
520 currSeqLen = 0;
521
522 for (int index = 0; index < buff.length(); index++) {
523 if (buff.charAt(index) == ':') {
524 if (currSeqLen != 0 && buff.charAt(index + 1) == '0') {
525 currSeqLen += 1;
526 }
527 } else if (buff.charAt(index) == '0' && ((index == 0) || (buff.charAt(index - 1) == ':'))) {
528 if (currSeqLen == 0) {
529 currSeqStart = index;
530 }
531 currSeqLen += 1;
532 } else {
533 if (currSeqLen > longestSeqLen) {
534 longestSeqStart = currSeqStart;
535 longestSeqLen = currSeqLen;
536 }
537 currSeqLen = 0;
538 }
539 }
540
541 if (currSeqLen > longestSeqLen) {
542 longestSeqLen = currSeqLen;
543 longestSeqStart = currSeqStart;
544 }
545 if (longestSeqLen > 1) {
546 if (buff.length() == (longestSeqStart + longestSeqLen)) {
547 buff.append(':');
548 }
549
550 buff.delete(longestSeqStart, longestSeqStart + longestSeqLen);
551
552 if (longestSeqStart == 0) {
553 buff.insert(0, ':');
554 }
555 }
556
557 return buff.toString();
558 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700559}