View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http2.frames;
20  
21  import java.util.Objects;
22  
23  public class PingFrame extends Frame
24  {
25      public static final int PING_LENGTH = 8;
26      private static final byte[] EMPTY_PAYLOAD = new byte[8];
27  
28      private final byte[] payload;
29      private final boolean reply;
30  
31      /**
32       * Creates a PING frame with an empty payload.
33       *
34       * @param reply whether this PING frame is a reply
35       */
36      public PingFrame(boolean reply)
37      {
38          this(EMPTY_PAYLOAD, reply);
39      }
40  
41      /**
42       * Creates a PING frame with the given {@code long} {@code value} as payload.
43       *
44       * @param value the value to use as a payload for this PING frame
45       * @param reply whether this PING frame is a reply
46       */
47      public PingFrame(long value, boolean reply)
48      {
49          this(toBytes(value), reply);
50      }
51  
52      /**
53       * Creates a PING frame with the given {@code payload}.
54       *
55       * @param payload the payload for this PING frame
56       * @param reply whether this PING frame is a reply
57       */
58      public PingFrame(byte[] payload, boolean reply)
59      {
60          super(FrameType.PING);
61          this.payload = Objects.requireNonNull(payload);
62          if (payload.length != PING_LENGTH)
63              throw new IllegalArgumentException("PING payload must be 8 bytes");
64          this.reply = reply;
65      }
66  
67      public byte[] getPayload()
68      {
69          return payload;
70      }
71  
72      public long getPayloadAsLong()
73      {
74          return toLong(payload);
75      }
76  
77      public boolean isReply()
78      {
79          return reply;
80      }
81  
82      private static byte[] toBytes(long value)
83      {
84          byte[] result = new byte[8];
85          for (int i = result.length - 1; i >= 0; --i)
86          {
87              result[i] = (byte)(value & 0xFF);
88              value >>= 8;
89          }
90          return result;
91      }
92  
93      private static long toLong(byte[] payload)
94      {
95          long result = 0;
96          for (int i = 0; i < 8; ++i)
97          {
98              result <<= 8;
99              result |= (payload[i] & 0xFF);
100         }
101         return result;
102     }
103 }