Decodes an integer in the HPACK prefix format. If the return value is -1 it means that there was not enough data in the buffer to complete the decoding sequence. If this method returns -1 then the source buffer will not have been modified. @param source The buffer that contains the integer @pa
(ByteBuffer source, int n)
| 153 | * @return The encoded integer, or -1 if there was not enough data |
| 154 | */ |
| 155 | static int decodeInteger(ByteBuffer source, int n) throws HpackException { |
| 156 | if (source.remaining() == 0) { |
| 157 | return -1; |
| 158 | } |
| 159 | int count = 1; |
| 160 | int sp = source.position(); |
| 161 | int mask = PREFIX_TABLE[n]; |
| 162 | |
| 163 | // Use long internally as the value may exceed Integer.MAX_VALUE |
| 164 | long result = mask & source.get(); |
| 165 | int b; |
| 166 | if (result < PREFIX_TABLE[n]) { |
| 167 | // Casting is safe as result must be less than 255 at this point. |
| 168 | return (int) result; |
| 169 | } else { |
| 170 | int m = 0; |
| 171 | do { |
| 172 | if (count++ > MAX_INTEGER_OCTETS) { |
| 173 | throw new HpackException( |
| 174 | sm.getString("hpack.integerEncodedOverTooManyOctets", Integer.valueOf(MAX_INTEGER_OCTETS))); |
| 175 | } |
| 176 | if (source.remaining() == 0) { |
| 177 | // we have run out of data |
| 178 | // reset |
| 179 | source.position(sp); |
| 180 | return -1; |
| 181 | } |
| 182 | b = source.get(); |
| 183 | result = result + (b & 127) * (PREFIX_TABLE[m] + 1L); |
| 184 | if (result > Integer.MAX_VALUE) { |
| 185 | throw new HpackException(sm.getString("hpack.integerEncodedTooBig")); |
| 186 | } |
| 187 | m += 7; |
| 188 | } while ((b & 128) == 128); |
| 189 | } |
| 190 | // Casting is safe as result must be less than Integer.MAX_VALUE at this point |
| 191 | return (int) result; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Encodes an integer in the HPACK prefix format. |