package mappedbus;

import mappedbus.MappedBus.Commit;
import mappedbus.MappedBus.Layout;
import mappedbus.MappedBus.Length;

public class MappedBusReader {

	private final MemoryMappedFile mem;

	private final long size;
	
	private long limit = Layout.Data;
	
	public MappedBusReader(String file, long size) {
		this.size = size;
		try {
			mem = new MemoryMappedFile(file, size);			
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public boolean hasNext() {
		if(limit >= size) {
			throw new RuntimeException("End of file was reached");
		}
		long newLimit = mem.getLongVolatile(Layout.Limit);
		return newLimit > limit;
	}
	
	private void next() {
		while (true) {
			if (mem.getIntVolatile(limit) == Commit.Set) {
				break;
			}
		}
		limit += Length.Commit;	
	}

	public int readType() {
		next();
		int type = mem.getInt(limit);
		limit += Length.Metadata;
		return type;
	}
	
	public int readBuffer(byte[] dst, int offset) {
		next();
		int length = mem.getInt(limit);
		limit += Length.Metadata;
		mem.getBytes(limit, dst, offset, length);
		limit += length;
		return length;
	}
	
	public void readMessage(Message message) {
		message.read(mem, limit);
		limit += message.size();
	}
}
@@ -1,8 +1,600 @@
+/* %0A* Copyright 2015 Caplogic AB. %0A* %0A* Licensed under the Apache License, Version 2.0 (the %22License%22); %0A* you may not use this file except in compliance with the License. %0A* You may obtain a copy of the License at %0A* %0A* http://www.apache.org/licenses/LICENSE-2.0 %0A* %0A* Unless required by applicable law or agreed to in writing, software %0A* distributed under the License is distributed on an %22AS IS%22 BASIS, %0A* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %0A* See the License for the specific language governing permissions and %0A* limitations under the License. %0A*/%0A
 package 
@@ -1800,8 +1800,9 @@
 ();%0A%09%7D%0A%7D
+%0A

Update MappedBusReader.java