Merge "API review: access field by method" into rvc-dev

This commit is contained in:
Aaron Huang
2020-03-20 11:09:43 +00:00
committed by Android (Google) Code Review
5 changed files with 66 additions and 32 deletions

View File

@@ -28,7 +28,7 @@ import java.lang.annotation.RetentionPolicy;
*/
@SystemApi
public class InvalidPacketException extends Exception {
public final int error;
private final int mError;
// Must match SocketKeepalive#ERROR_INVALID_IP_ADDRESS.
/** Invalid IP address. */
@@ -56,6 +56,11 @@ public class InvalidPacketException extends Exception {
* See the error code for details.
*/
public InvalidPacketException(@ErrorCode final int error) {
this.error = error;
this.mError = error;
}
/** Get error code. */
public int getError() {
return mError;
}
}

View File

@@ -19,6 +19,7 @@ package android.net;
import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS;
import static android.net.InvalidPacketException.ERROR_INVALID_PORT;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.net.util.IpUtils;
@@ -37,17 +38,17 @@ public class KeepalivePacketData {
/** Source IP address */
@NonNull
public final InetAddress srcAddress;
private final InetAddress mSrcAddress;
/** Destination IP address */
@NonNull
public final InetAddress dstAddress;
private final InetAddress mDstAddress;
/** Source port */
public final int srcPort;
private final int mSrcPort;
/** Destination port */
public final int dstPort;
private final int mDstPort;
/** Packet data. A raw byte string of packet data, not including the link-layer header. */
private final byte[] mPacket;
@@ -60,13 +61,14 @@ public class KeepalivePacketData {
/**
* A holding class for data necessary to build a keepalive packet.
*/
protected KeepalivePacketData(@NonNull InetAddress srcAddress, int srcPort,
@NonNull InetAddress dstAddress, int dstPort,
@NonNull byte[] data) throws InvalidPacketException {
this.srcAddress = srcAddress;
this.dstAddress = dstAddress;
this.srcPort = srcPort;
this.dstPort = dstPort;
protected KeepalivePacketData(@NonNull InetAddress srcAddress,
@IntRange(from = 0, to = 65535) int srcPort, @NonNull InetAddress dstAddress,
@IntRange(from = 0, to = 65535) int dstPort,
@NonNull byte[] data) throws InvalidPacketException {
this.mSrcAddress = srcAddress;
this.mDstAddress = dstAddress;
this.mSrcPort = srcPort;
this.mDstPort = dstPort;
this.mPacket = data;
// Check we have two IP addresses of the same family.
@@ -83,6 +85,31 @@ public class KeepalivePacketData {
}
}
/** Get source IP address. */
@NonNull
public InetAddress getSrcAddress() {
return mSrcAddress;
}
/** Get destination IP address. */
@NonNull
public InetAddress getDstAddress() {
return mDstAddress;
}
/** Get source port number. */
public int getSrcPort() {
return mSrcPort;
}
/** Get destination port number. */
public int getDstPort() {
return mDstPort;
}
/**
* Returns a byte array of the given packet data.
*/
@NonNull
public byte[] getPacket() {
return mPacket.clone();

View File

@@ -94,10 +94,10 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
/** Write to parcel */
public void writeToParcel(@NonNull Parcel out, int flags) {
out.writeString(srcAddress.getHostAddress());
out.writeString(dstAddress.getHostAddress());
out.writeInt(srcPort);
out.writeInt(dstPort);
out.writeString(getSrcAddress().getHostAddress());
out.writeString(getDstAddress().getHostAddress());
out.writeInt(getSrcPort());
out.writeInt(getDstPort());
}
/** Parcelable Creator */
@@ -115,7 +115,7 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
dstAddress, dstPort);
} catch (InvalidPacketException e) {
throw new IllegalArgumentException(
"Invalid NAT-T keepalive data: " + e.error);
"Invalid NAT-T keepalive data: " + e.getError());
}
}
@@ -128,14 +128,16 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
public boolean equals(@Nullable final Object o) {
if (!(o instanceof NattKeepalivePacketData)) return false;
final NattKeepalivePacketData other = (NattKeepalivePacketData) o;
return this.srcAddress.equals(other.srcAddress)
&& this.dstAddress.equals(other.dstAddress)
&& this.srcPort == other.srcPort
&& this.dstPort == other.dstPort;
final InetAddress srcAddress = getSrcAddress();
final InetAddress dstAddress = getDstAddress();
return srcAddress.equals(other.getSrcAddress())
&& dstAddress.equals(other.getDstAddress())
&& getSrcPort() == other.getSrcPort()
&& getDstPort() == other.getDstPort();
}
@Override
public int hashCode() {
return Objects.hash(srcAddress, dstAddress, srcPort, dstPort);
return Objects.hash(getSrcAddress(), getDstAddress(), getSrcPort(), getDstPort());
}
}