@Test public void testEncodeLength() throws Exception { Assert.assertArrayEquals(new byte[]{0x00}, SecurityTokenUtils.encodeLength(0)); Assert.assertArrayEquals(new byte[]{0x01}, SecurityTokenUtils.encodeLength(1)); Assert.assertArrayEquals(new byte[]{0x7f}, SecurityTokenUtils.encodeLength(127)); Assert.assertArrayEquals(new byte[]{(byte) 0x81, (byte) 0x80}, SecurityTokenUtils.encodeLength(128)); Assert.assertArrayEquals(new byte[]{(byte) 0x81, (byte) 0xFF}, SecurityTokenUtils.encodeLength(255)); Assert.assertArrayEquals(new byte[]{(byte) 0x82, (byte) 0x01, 0x00}, SecurityTokenUtils.encodeLength(256)); Assert.assertArrayEquals(new byte[]{(byte) 0x82, (byte) 0xFF, (byte) 0xFF}, SecurityTokenUtils.encodeLength(65535)); Assert.assertArrayEquals(new byte[]{(byte) 0x83, (byte) 0x01, (byte) 0x00, (byte) 0x00}, SecurityTokenUtils.encodeLength(65536)); Assert.assertArrayEquals(new byte[]{(byte) 0x83, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, SecurityTokenUtils.encodeLength(16777215)); }
static byte[] encodeLength(int len) { if (len < 0) { throw new IllegalArgumentException("length is negative"); } else if (len >= 16777216) { throw new IllegalArgumentException("length is too big: " + len); } byte[] res; if (len < 128) { res = new byte[1]; res[0] = (byte) len; } else if (len < 256) { res = new byte[2]; res[0] = -127; res[1] = (byte) len; } else if (len < 65536) { res = new byte[3]; res[0] = -126; res[1] = (byte) (len / 256); res[2] = (byte) (len % 256); } else { res = new byte[4]; res[0] = -125; res[1] = (byte) (len / 65536); res[2] = (byte) (len / 256); res[3] = (byte) (len % 256); } return res; }
SecurityTokenUtils { static byte[] encodeLength(int len) { if (len < 0) { throw new IllegalArgumentException("length is negative"); } else if (len >= 16777216) { throw new IllegalArgumentException("length is too big: " + len); } byte[] res; if (len < 128) { res = new byte[1]; res[0] = (byte) len; } else if (len < 256) { res = new byte[2]; res[0] = -127; res[1] = (byte) len; } else if (len < 65536) { res = new byte[3]; res[0] = -126; res[1] = (byte) (len / 256); res[2] = (byte) (len % 256); } else { res = new byte[4]; res[0] = -125; res[1] = (byte) (len / 65536); res[2] = (byte) (len / 256); res[3] = (byte) (len % 256); } return res; } }
SecurityTokenUtils { static byte[] encodeLength(int len) { if (len < 0) { throw new IllegalArgumentException("length is negative"); } else if (len >= 16777216) { throw new IllegalArgumentException("length is too big: " + len); } byte[] res; if (len < 128) { res = new byte[1]; res[0] = (byte) len; } else if (len < 256) { res = new byte[2]; res[0] = -127; res[1] = (byte) len; } else if (len < 65536) { res = new byte[3]; res[0] = -126; res[1] = (byte) (len / 256); res[2] = (byte) (len % 256); } else { res = new byte[4]; res[0] = -125; res[1] = (byte) (len / 65536); res[2] = (byte) (len / 256); res[3] = (byte) (len % 256); } return res; }  }
SecurityTokenUtils { static byte[] encodeLength(int len) { if (len < 0) { throw new IllegalArgumentException("length is negative"); } else if (len >= 16777216) { throw new IllegalArgumentException("length is too big: " + len); } byte[] res; if (len < 128) { res = new byte[1]; res[0] = (byte) len; } else if (len < 256) { res = new byte[2]; res[0] = -127; res[1] = (byte) len; } else if (len < 65536) { res = new byte[3]; res[0] = -126; res[1] = (byte) (len / 256); res[2] = (byte) (len % 256); } else { res = new byte[4]; res[0] = -125; res[1] = (byte) (len / 65536); res[2] = (byte) (len / 256); res[3] = (byte) (len % 256); } return res; }  static byte[] attributesFromSecretKey(KeyType slot, CanonicalizedSecretKey secretKey,
            KeyFormat formatForKeyType); static byte[] createRSAPrivKeyTemplate(RSAPrivateCrtKey secretKey, KeyType slot,
            RSAKeyFormat format); static byte[] createECPrivKeyTemplate(ECPrivateKey secretKey, ECPublicKey publicKey, KeyType slot,
            ECKeyFormat format); }
SecurityTokenUtils { static byte[] encodeLength(int len) { if (len < 0) { throw new IllegalArgumentException("length is negative"); } else if (len >= 16777216) { throw new IllegalArgumentException("length is too big: " + len); } byte[] res; if (len < 128) { res = new byte[1]; res[0] = (byte) len; } else if (len < 256) { res = new byte[2]; res[0] = -127; res[1] = (byte) len; } else if (len < 65536) { res = new byte[3]; res[0] = -126; res[1] = (byte) (len / 256); res[2] = (byte) (len % 256); } else { res = new byte[4]; res[0] = -125; res[1] = (byte) (len / 65536); res[2] = (byte) (len / 256); res[3] = (byte) (len % 256); } return res; }  static byte[] attributesFromSecretKey(KeyType slot, CanonicalizedSecretKey secretKey,
            KeyFormat formatForKeyType); static byte[] createRSAPrivKeyTemplate(RSAPrivateCrtKey secretKey, KeyType slot,
            RSAKeyFormat format); static byte[] createECPrivKeyTemplate(ECPrivateKey secretKey, ECPublicKey publicKey, KeyType slot,
            ECKeyFormat format);  }