(String text, int length)
| 82 | * 2. Use specific named charset |
| 83 | */ |
| 84 | private byte[] encodeString(String text, int length) { |
| 85 | |
| 86 | final Charset charSet = getCharset(); |
| 87 | |
| 88 | // Convert to bytes using given encoding, and align *bytes* to multiple of |
| 89 | // 8, with 4 bytes reserved for the crc |
| 90 | final byte[] bytes = text.getBytes(charSet); |
| 91 | int align = (bytes.length + 4) % length; |
| 92 | int paddingNeeded = length - align; |
| 93 | final byte[] paddedBytes = Arrays.copyOf(bytes, bytes.length + paddingNeeded); |
| 94 | |
| 95 | // Now calculate the crc, using the bytes |
| 96 | String crc = crcHeader(paddedBytes); |
| 97 | |
| 98 | byte[] crcBytes = crc.getBytes(charSet); |
| 99 | if(crcBytes.length != 4) { |
| 100 | System.err.println("CRC Bytes really should be 4 in length!"); |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | // Now combine crc bytes and string bytes into byte array |
| 105 | // for encryption |
| 106 | byte[] total = new byte[paddedBytes.length + crcBytes.length]; |
| 107 | System.arraycopy(crcBytes, 0, total, 0, crcBytes.length); |
| 108 | System.arraycopy(paddedBytes, 0, total, crcBytes.length, paddedBytes.length); |
| 109 | |
| 110 | return total; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
no test coverage detected