package ru.r2cloud.jradio.blocks;

import java.io.IOException;

import ru.r2cloud.jradio.ByteInput;
import ru.r2cloud.jradio.Context;
import ru.r2cloud.jradio.MessageInput;
import ru.r2cloud.jradio.Tag;

public class TaggedStreamToPdu implements MessageInput {

	private final ByteInput input;

	public TaggedStreamToPdu(ByteInput input) {
		this.input = input;
	}

	@Override
	public byte[] readBytes() throws IOException {
		Tag tag = null;
		byte firstByte;
		do {
			// discard bytes
			firstByte = input.readByte();
			tag = getContext().getCurrent();
			if (tag != null) {
				break;
			}
		} while (true);

		@SuppressWarnings("null")
		int length = (Integer) tag.get(FixedLengthTagger.LENGTH);
		byte[] result = new byte[length];
		result[0] = firstByte;
		for (int i = 1; i < length; i++) {
			result[i] = input.readByte();
		}
		getContext().setCurrent(tag);
		return result;
	}

	@Override
	public void close() throws IOException {
		input.close();
	}
	
	@Override
	public Context getContext() {
		return input.getContext();
	}

}

@@ -28,16 +28,45 @@
 locks;%0A%0A
+import java.io.EOFException;%0A
 import j
@@ -483,16 +483,41 @@
 stByte;%0A
+%09%09Integer length = null;%0A
 %09%09do %7B%0A%09
@@ -634,81 +634,196 @@
 %09%09%09%09
-break;%0A%09%09%09%7D%0A%09%09%7D while (true);%0A%0A%09%09@SuppressWarnings(%22null%22)
+length = (Integer) tag.get(FixedLengthTagger.LENGTH);%0A%09%09%09%09if (length != null) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D while (true);%0A%0A%09%09// length cannot be null due to while(true) above
 %0A%09%09i
-nt 
+f (
 length =
  (In
@@ -822,53 +822,51 @@
 th =
- (Integer) tag.get(FixedLengthTagger.LENGTH);
+= null) %7B%0A%09%09%09throw new EOFException();%0A%09%09%7D%0A
 %0A%09%09b
@@ -1123,17 +1123,16 @@
 e();%0A%09%7D%0A
-%09
 %0A%09@Overr

fix NPE