/* * 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 org.f1x.log.file.nio; import org.f1x.log.MessageLog; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; /** * Message logger that keeps last <code>maxSize</code> bytes of FIX traffic. * Fixed size logger backed by memory mapped file. */ public class MemMappedMessageLogger implements MessageLog { private final RandomAccessFile raf; private final MappedByteBuffer out; /** * * @param file * @param maxSize This setting defines maximum size of log file. Leave at zero to disable this feature. */ public MemMappedMessageLogger (File file, int maxSize) { try { raf = new RandomAccessFile(file, "rw"); out = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, maxSize); } catch (IOException e) { throw new RuntimeException("Error opening log file " + file.getAbsolutePath(), e); } } @Override public synchronized void log(boolean isInbound, byte[] buffer, int offset, int length) { final int remaining = out.remaining(); if (remaining < length) { if (remaining > 0) out.put(buffer, offset, remaining); out.position(0); out.put(buffer, offset+remaining, length - remaining); } else { out.put(buffer, offset, length); } if (out.remaining() > 0) out.put((byte)'\n'); } @Override public synchronized void close() throws IOException { out.force(); raf.close(); } }