// Path: libraries/core/src/main/java/com/paritytrading/nassau/moldudp64/MoldUDP64.java
// class MoldUDP64 {
// 
//     static final int MESSAGE_COUNT_HEARTBEAT = 0x0;
// 
//     static final int MESSAGE_COUNT_END_OF_SESSION = 0xFFFF;
// 
//     static final int MAX_MESSAGE_COUNT = MESSAGE_COUNT_END_OF_SESSION - 1;
// 
//     static final int HEADER_LENGTH = 20;
// 
//     static final int MAX_PAYLOAD_LENGTH = 1400;
// 
//     static final int SESSION_LENGTH = 10;
// 
//     private static final byte SPACE = ' ';
// 
//     static byte[] session(String name) {
//         byte[] session = new byte[SESSION_LENGTH];
//         fill(session, SPACE);
// 
//         byte[] bytes = name.getBytes(US_ASCII);
//         System.arraycopy(bytes, 0, session, 0, Math.min(bytes.length, session.length));
// 
//         return session;
//     }
// 
// }
// 
// Path: libraries/core/src/main/java/com/paritytrading/nassau/MessageListener.java
// public interface MessageListener {
// 
//     /**
//      * Receive a message. The message is contained in the buffer between the
//      * current position and the limit.
//      *
//      * @param buffer a buffer
//      * @throws IOException if an I/O error occurs
//      */
//     void message(ByteBuffer buffer) throws IOException;
// 
// }


import static com.paritytrading.foundation.ByteBuffers.*;
import static com.paritytrading.nassau.moldudp64.MoldUDP64.*;
import com.paritytrading.nassau.MessageListener;
import java.io.IOException;
import java.nio.ByteBuffer;
/*
 * Copyright 2014 Nassau authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.paritytrading.nassau.moldudp64;



/**
 * An implementation of a MoldUDP64 downstream packet.
 */
public class MoldUDP64DownstreamPacket {

    private int messageCount;

    private final ByteBuffer payload;

    /**
     * Create a downstream packet.
     */
    public MoldUDP64DownstreamPacket() {
        this.messageCount = 0;

        this.payload = ByteBuffer.allocateDirect(MAX_PAYLOAD_LENGTH);
    }

    /**
     * Append a message to this downstream packet.
     *
     * @param buffer a buffer containing a message
     * @throws MoldUDP64Exception if the message is too large
     */
    public void put(ByteBuffer buffer) throws MoldUDP64Exception {
        if (remaining() < buffer.remaining())
            throw new MoldUDP64Exception("Buffer overflow");

        putUnsignedShort(payload, buffer.remaining());
        payload.put(buffer);

        messageCount++;
    }

    /**
     * Apply the message listener to each message in this downstream packet.
     *
     * @param listener a message listener
     * @throws IOException if an I/O error occurs in the message listener
     */
// Path: libraries/core/src/main/java/com/paritytrading/nassau/moldudp64/MoldUDP64.java
// class MoldUDP64 {
// 
//     static final int MESSAGE_COUNT_HEARTBEAT = 0x0;
// 
//     static final int MESSAGE_COUNT_END_OF_SESSION = 0xFFFF;
// 
//     static final int MAX_MESSAGE_COUNT = MESSAGE_COUNT_END_OF_SESSION - 1;
// 
//     static final int HEADER_LENGTH = 20;
// 
//     static final int MAX_PAYLOAD_LENGTH = 1400;
// 
//     static final int SESSION_LENGTH = 10;
// 
//     private static final byte SPACE = ' ';
// 
//     static byte[] session(String name) {
//         byte[] session = new byte[SESSION_LENGTH];
//         fill(session, SPACE);
// 
//         byte[] bytes = name.getBytes(US_ASCII);
//         System.arraycopy(bytes, 0, session, 0, Math.min(bytes.length, session.length));
// 
//         return session;
//     }
// 
// }
// 
// Path: libraries/core/src/main/java/com/paritytrading/nassau/MessageListener.java
// public interface MessageListener {
// 
//     /**
//      * Receive a message. The message is contained in the buffer between the
//      * current position and the limit.
//      *
//      * @param buffer a buffer
//      * @throws IOException if an I/O error occurs
//      */
//     void message(ByteBuffer buffer) throws IOException;
// 
// }


// Path: libraries/core/src/main/java/com/paritytrading/nassau/moldudp64/MoldUDP64DownstreamPacket.java
import static com.paritytrading.foundation.ByteBuffers.*;
import static com.paritytrading.nassau.moldudp64.MoldUDP64.*;
import com.paritytrading.nassau.MessageListener;
import java.io.IOException;
import java.nio.ByteBuffer;

/*
 * Copyright 2014 Nassau authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.paritytrading.nassau.moldudp64;



/**
 * An implementation of a MoldUDP64 downstream packet.
 */
public class MoldUDP64DownstreamPacket {

    private int messageCount;

    private final ByteBuffer payload;

    /**
     * Create a downstream packet.
     */
    public MoldUDP64DownstreamPacket() {
        this.messageCount = 0;

        this.payload = ByteBuffer.allocateDirect(MAX_PAYLOAD_LENGTH);
    }

    /**
     * Append a message to this downstream packet.
     *
     * @param buffer a buffer containing a message
     * @throws MoldUDP64Exception if the message is too large
     */
    public void put(ByteBuffer buffer) throws MoldUDP64Exception {
        if (remaining() < buffer.remaining())
            throw new MoldUDP64Exception("Buffer overflow");

        putUnsignedShort(payload, buffer.remaining());
        payload.put(buffer);

        messageCount++;
    }

    /**
     * Apply the message listener to each message in this downstream packet.
     *
     * @param listener a message listener
     * @throws IOException if an I/O error occurs in the message listener
     */
    public void apply(MessageListener listener) throws IOException {