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
;
12 class AppEngineSocketUtils
{
13 private static final int IPV6_LEN
= 16;
14 private static final int IPV4_LEN
= 4;
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
) {
30 if (bytes
.length
== IPV4_LEN
) {
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() {}