View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.journal.impl;
    19
    20  import java.nio.ByteBuffer;
    21
    22
    23  /***
    24   * Serializes/Deserializes data records.
    25   *
    26   * @version $Revision: 1.2 $
    27   */
    28  public class Record {
    29
    30  	static final public int RECORD_BASE_SIZE=RecordHeader.RECORD_HEADER_SIZE+RecordFooter.RECORD_FOOTER_SIZE;
    31
    32  	final RecordHeader header = new RecordHeader();
    33  	final RecordFooter footer = new RecordFooter();
    34  	final ByteBuffer headerBuffer;
    35  	final ByteBuffer dataBuffer;
    36  	final ByteBuffer footerBuffer;
    37  	final Mark mark;
    38
    39  	public Record(RecordHeader header, byte[] data) {
    40  		this( header.sequenceId, header.recordType, data, null);
    41  	}
    42
    43  	public Record(final long sequenceId, final byte recordType, final byte data[], final Mark visitData) {
    44  		this.header.bulkSet(recordType, sequenceId, data);
    45  	    this.headerBuffer = this.header.toByteBuffer();
    46  	    this.dataBuffer = ByteBuffer.wrap( data );
    47  	    this.footer.bulkSet(header, data);
    48  	    this.footerBuffer = footer.toByteBuffer();
    49  	    this.mark = visitData;
    50  	}
    51
    52
    53  	public RecordHeader getHeader() {
    54  		return header;
    55  	}
    56
    57  	public Mark getMark() {
    58  		return mark;
    59  	}
    60
    61  	public int remaining() {
    62  		return headerBuffer.remaining() + dataBuffer.remaining();
    63  	}
    64
    65  	public void fill(ByteBuffer byteBuffer) {
    66  		fill( byteBuffer, headerBuffer);
    67  		fill( byteBuffer, dataBuffer);
    68  		fill( byteBuffer, footerBuffer);
    69  	}
    70
    71  	/***
    72  	 * Relative bulk put operation that copies bytes from src to dest.  Unlike
    73  	 * <code>ByteBuffer.put(ByteBuffer)</code> this will not throw a
    74  	 * <code>java.nio.BufferOverflowException</code>.  It will just put
    75  	 * as many bytes as can fit into the dest object.
    76  	 *
    77  	 * @param byteBuffer
    78  	 * @param headerBuffer2
    79  	 */
    80  	private void fill(ByteBuffer dest, ByteBuffer src) {
    81  		int r =dest.remaining();
    82  		if( r ==0 )
    83  			return;
    84  		// If the src won't fit into the dest.
    85  		if( r < src.remaining() ) {
    86  			// temporarly reduce the limit on the src so it does fit.
    87  			int limit = src.limit();
    88  			src.limit(src.position()+r);
    89  			dest.put(src);
    90  			// restore the limit.
    91  			src.limit(limit);
    92  		} else {
    93  			dest.put(src);
    94  		}
    95
    96  	}
    97
    98  }