Convert the given bytes to characters. @param bc byte input @param cc char output @param endOfInput Is this all of the available data @throws IOException If the conversion can not be completed
(ByteChunk bc, CharChunk cc, boolean endOfInput)
| 135 | * @throws IOException If the conversion can not be completed |
| 136 | */ |
| 137 | public void convert(ByteChunk bc, CharChunk cc, boolean endOfInput) throws IOException { |
| 138 | if ((bb == null) || (bb.array() != bc.getBuffer())) { |
| 139 | // Create a new byte buffer if anything changed |
| 140 | bb = ByteBuffer.wrap(bc.getBuffer(), bc.getStart(), bc.getLength()); |
| 141 | } else { |
| 142 | // Initialize the byte buffer |
| 143 | bb.limit(bc.getEnd()); |
| 144 | bb.position(bc.getStart()); |
| 145 | } |
| 146 | if ((cb == null) || (cb.array() != cc.getBuffer())) { |
| 147 | // Create a new char buffer if anything changed |
| 148 | cb = CharBuffer.wrap(cc.getBuffer(), cc.getEnd(), cc.getBuffer().length - cc.getEnd()); |
| 149 | } else { |
| 150 | // Initialize the char buffer |
| 151 | cb.limit(cc.getBuffer().length); |
| 152 | cb.position(cc.getEnd()); |
| 153 | } |
| 154 | CoderResult result; |
| 155 | // Parse leftover if any are present |
| 156 | if (leftovers.position() > 0) { |
| 157 | do { |
| 158 | // Previous call may have triggered overflow in cb so try conversion first |
| 159 | leftovers.flip(); |
| 160 | result = decoder.decode(leftovers, cb, false); |
| 161 | if (result.isOverflow()) { |
| 162 | leftovers.position(leftovers.limit()); |
| 163 | leftovers.limit(leftovers.array().length); |
| 164 | return; |
| 165 | } else if (result.isError()) { |
| 166 | result.throwException(); |
| 167 | } else if (leftovers.remaining() == 0) { |
| 168 | // leftovers have been converted |
| 169 | leftovers.clear(); |
| 170 | bb.position(bc.getStart()); |
| 171 | break; |
| 172 | } else { |
| 173 | // leftovers is incomplete |
| 174 | leftovers.position(leftovers.limit()); |
| 175 | leftovers.limit(leftovers.array().length); |
| 176 | } |
| 177 | // Try |
| 178 | int b = bc.subtract(); |
| 179 | if (b < 0) { |
| 180 | if (endOfInput) { |
| 181 | throw new MalformedInputException(leftovers.position()); |
| 182 | } |
| 183 | // Underflow - need more bytes to complete the leftover character |
| 184 | return; |
| 185 | } |
| 186 | leftovers.put((byte) b); |
| 187 | } while (true); |
| 188 | } |
| 189 | // Do the decoding and get the results into the byte chunk and the char |
| 190 | // chunk |
| 191 | result = decoder.decode(bb, cb, endOfInput); |
| 192 | if (result.isError()) { |
| 193 | result.throwException(); |
| 194 | } else if (result.isOverflow()) { |