1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / socket / AppEngineSocketUtils.java
blobac40ba4c77a745bfad919c213059e887e20a6308
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 package com.google.appengine.api.socket;
4 import com.google.appengine.api.socket.SocketServicePb.AddressPort;
6 import java.net.InetAddress;
8 /**
9 * Utility methods.
12 class AppEngineSocketUtils {
13 private static final int IPV6_LEN = 16;
14 private static final int IPV4_LEN = 4;
16 /**
17 * Convert an inet address and port to an IPv6 AddressPort. IPv4 addresses
18 * are mapped to the IPv6 compatible address.
20 static AddressPort toAddressPort(InetAddress address, int port) {
21 return new AddressPort().setPort(port).setPackedAddressAsBytes(addrAsIpv6Bytes(address));
24 static byte[] addrAsIpv6Bytes(InetAddress address) {
25 byte[] bytes = address.getAddress();
26 if (bytes.length == IPV6_LEN) {
27 return bytes;
30 if (bytes.length == IPV4_LEN) {
31 return new byte[] {
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, bytes[0], bytes[1], bytes[2], bytes[3]};
34 throw new IllegalArgumentException(
35 "address must be IPV4 or IPV6 - unknown size: " + bytes.length);
38 private AppEngineSocketUtils() {}