001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.jlan; 029 030/** 031 * Buffer class which holds file contents for JLAN file access in memory before they are written to the VFS.<p> 032 * 033 * This is implemented as a CmsByteBuffer instance together with a 'position' index which marks the next write position 034 */ 035public class CmsFileBuffer { 036 037 /** The buffer used to store the file contents. */ 038 CmsByteBuffer m_buffer = new CmsByteBuffer(8192); 039 040 /** The current write position. */ 041 long m_position; 042 043 /** 044 * Gets the contents of this buffer as a byte array.<p> 045 * 046 * @return the file content 047 */ 048 public byte[] getContents() { 049 050 byte[] contents = new byte[m_buffer.size()]; 051 m_buffer.readBytes(contents, 0, 0, m_buffer.size()); 052 return contents; 053 } 054 055 /** 056 * Gets the length of the file content.<p> 057 * 058 * @return the content length 059 * 060 */ 061 public long getLength() { 062 063 return m_buffer.size(); 064 } 065 066 /** 067 * Gets the current write position.<p> 068 * 069 * @return the current write position 070 */ 071 public long getPosition() { 072 073 return m_position; 074 } 075 076 /** 077 * Initializes the file content data.<p> 078 * 079 * @param data the file content data 080 */ 081 public void init(byte[] data) { 082 083 m_position = 0; 084 m_buffer.writeBytes(data, 0, 0, data.length); 085 } 086 087 /** 088 * Transfers data from this buffer to a byte array.<p> 089 * 090 * @param dest the target byte array 091 * 092 * @param length the number of bytes to transfer 093 * @param bufferOffset the start index for the target buffer 094 * @param fileOffset the start index for this instance 095 * 096 * @return the number of bytes read, or -1 if we are at the end of the file 097 */ 098 public int read(byte[] dest, int length, int bufferOffset, int fileOffset) { 099 100 if (fileOffset >= m_buffer.size()) { 101 return -1; 102 } 103 int readEnd = fileOffset + length; 104 if (readEnd > m_buffer.size()) { 105 length = length - (readEnd - m_buffer.size()); 106 } 107 m_buffer.readBytes(dest, fileOffset, bufferOffset, length); 108 return length; 109 } 110 111 /** 112 * Changes the write position.<p> 113 * 114 * @param newPos the new write position 115 */ 116 public void seek(long newPos) { 117 118 m_position = newPos; 119 } 120 121 /** 122 * Changes the size of this buffer.<p> 123 * 124 * @param size the new size 125 */ 126 public void truncate(int size) { 127 128 m_buffer.truncate(size); 129 m_position = Math.min(size, m_position); 130 } 131 132 /** 133 * Writes the data to the internal buffer.<p> 134 * 135 * @param data the data to write 136 */ 137 public void write(byte[] data) { 138 139 m_buffer.writeBytes(data, 0, (int)m_position, data.length); 140 } 141}