protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY); if (decoderState == null) { decoderState = new DecoderState(); session.setAttribute(DECODER_STATE_KEY, decoderState); } if (decoderState.image1 == null) { // try to read first image if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) { decoderState.image1 = readImage(in); } else { // not enough data available to read first image return false; } } if (decoderState.image1 != null) { // try to read second image if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) { BufferedImage image2 = readImage(in); ImageResponse imageResponse = new ImageResponse(decoderState.image1, image2); out.write(imageResponse); decoderState.image1 = null; return true; } else { // not enough data available to read second image return false; } } return false; }
private void initializeStateQueues( List<MinPriorityDecoderQueue<DecoderState>> decoderStateQueues, int sentenceLength) { DecoderState startState = getStartState(sentenceLength); for (int i = 0; i <= sentenceLength; ++i) { MinPriorityDecoderQueue<DecoderState> queue = new MinPriorityDecoderQueue<DecoderState>(BeamSizeForStatesQueue); decoderStateQueues.add(queue); } decoderStateQueues.get(0).insert(startState, startState.getScore()); }
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) { // Get the state of the current message and // Skip what we have already scanned DecoderState state = decoderState(session); in.position(state.current()); while (in.hasRemaining()) { byte current = in.get(); // If it is the start byte and mark the position if (current == config.getStartByte()) { state.markStart(in.position() - 1); } // If it is the end bytes, extract the payload and return if (state.previous() == config.getEndByte1() && current == config.getEndByte2()) { // Remember the current position and limit. int position = in.position(); int limit = in.limit(); LOG.debug("Message ends at position {} with length {}", position, position - state.start()); try { in.position(state.start()); in.limit(position); // The bytes between in.position() and in.limit() // now contain a full MLLP message including the // start and end bytes. out.write( config.isProduceString() ? parseMessageToString(in.slice(), charsetDecoder(session)) : parseMessageToByteArray(in.slice())); } catch (CharacterCodingException cce) { throw new IllegalArgumentException("Exception while finalizing the message", cce); } finally { // Reset position, limit, and state in.limit(limit); in.position(position); state.reset(); } return true; } // Remember previous byte in state object because the buffer could // be theoretically exhausted right between the two end bytes state.markPrevious(current); } // Could not find a complete message in the buffer. // Reset to the initial position and return false so that this method // is called again with more data. LOG.debug("No complete message yet at position {} ", in.position()); state.markCurrent(in.position()); in.position(0); return false; }
private void UpdateStateQueue( MinPriorityDecoderQueue<DecoderState> minPriorityDecoderQueue, DecoderState curState, DecoderState prevState, ScoredPhrasePairForSentence phrase, HashMap<DecoderState, BackPointerElement> backPointers) { BackPointerElement elem = backPointers.get(curState); if (elem == null || elem.getState().getScore() < curState.getScore()) { minPriorityDecoderQueue.insert(curState, curState.getScore()); if (elem == null) { backPointers.put(curState, new BackPointerElement(prevState, phrase)); } else { elem.setState(prevState); elem.setPhrase(phrase); } } }
private List<ScoredPhrasePairForSentence> readBackPointers( DecoderState state, List<HashMap<DecoderState, BackPointerElement>> backPointersList, int startLength, int sentenceLength) { DecoderState curState = state; DecoderState startState = getStartState(sentenceLength); int curLength = startLength; List<ScoredPhrasePairForSentence> phrases = new ArrayList<>(); while (!curState.equals(startState)) { BackPointerElement backPointer = backPointersList.get(curLength).get(curState); phrases.add(0, backPointer.getPhrase()); curState = backPointer.getState(); curLength = decoderModel.getDecodedLength(curState); } return phrases; }