| | |
| | | package com.ruoyi.device.bts; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.EOFException; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | throw new BtsTransportException(BtsTransportException.Reason.DISCONNECTED, |
| | | "BTS disconnected before completing a response for " + host + ":" + port, ex); |
| | | } |
| | | catch (ResponseTooLargeException ex) |
| | | catch (BtsFrameReader.FrameTooLargeException ex) |
| | | { |
| | | closeSocket(); |
| | | throw new BtsTransportException(BtsTransportException.Reason.RESPONSE_TOO_LARGE, |
| | |
| | | |
| | | private String readFrame(InputStream input) throws IOException |
| | | { |
| | | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| | | int matched = 0; |
| | | while (true) |
| | | { |
| | | int value = input.read(); |
| | | if (value < 0) throw new EOFException("BTS closed the connection before the response frame ended"); |
| | | if ((byte) value == FRAME_END[matched]) |
| | | { |
| | | matched++; |
| | | if (matched == FRAME_END.length) return decodeUtf8(buffer.toByteArray()); |
| | | continue; |
| | | } |
| | | if (matched > 0) |
| | | { |
| | | for (int i = 0; i < matched; i++) appendPayload(buffer, FRAME_END[i]); |
| | | matched = 0; |
| | | if ((byte) value == FRAME_END[0]) |
| | | { |
| | | matched = 1; |
| | | continue; |
| | | } |
| | | } |
| | | appendPayload(buffer, value); |
| | | } |
| | | } |
| | | |
| | | private void appendPayload(ByteArrayOutputStream buffer, int value) throws ResponseTooLargeException |
| | | { |
| | | if (buffer.size() >= maxResponseBytes) throw new ResponseTooLargeException(); |
| | | buffer.write(value); |
| | | return decodeUtf8(BtsFrameReader.read(input, maxResponseBytes)); |
| | | } |
| | | |
| | | private static String decodeUtf8(byte[] bytes) throws InvalidUtf8Exception |
| | |
| | | { |
| | | socket = null; |
| | | } |
| | | } |
| | | |
| | | private static final class ResponseTooLargeException extends IOException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | } |
| | | |
| | | private static final class InvalidUtf8Exception extends IOException |