001    /* ============================================================
002     * JRobin : Pure java implementation of RRDTool's functionality
003     * ============================================================
004     *
005     * Project Info:  http://www.jrobin.org
006     * Project Lead:  Sasa Markovic (saxon@jrobin.org);
007     *
008     * (C) Copyright 2003-2005, by Sasa Markovic.
009     *
010     * Developers:    Sasa Markovic (saxon@jrobin.org)
011     *
012     *
013     * This library is free software; you can redistribute it and/or modify it under the terms
014     * of the GNU Lesser General Public License as published by the Free Software Foundation;
015     * either version 2.1 of the License, or (at your option) any later version.
016     *
017     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019     * See the GNU Lesser General Public License for more details.
020     *
021     * You should have received a copy of the GNU Lesser General Public License along with this
022     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023     * Boston, MA 02111-1307, USA.
024     */
025    
026    package org.jrobin.core;
027    
028    import java.io.IOException;
029    
030    /**
031     * Backend to be used to store all RRD bytes in memory.<p>
032     */
033    public class RrdMemoryBackend extends RrdBackend {
034            private byte[] buffer = new byte[0];
035    
036            protected RrdMemoryBackend(String path) {
037                    super(path);
038            }
039    
040            protected synchronized void write(long offset, byte[] b) throws IOException {
041                    int pos = (int) offset;
042                    for (byte singleByte : b) {
043                            buffer[pos++] = singleByte;
044                    }
045            }
046    
047            protected synchronized void read(long offset, byte[] b) throws IOException {
048                    int pos = (int) offset;
049                    if (pos + b.length <= buffer.length) {
050                            for (int i = 0; i < b.length; i++) {
051                                    b[i] = buffer[pos++];
052                            }
053                    }
054                    else {
055                            throw new IOException("Not enough bytes available in memory " + getPath());
056                    }
057            }
058    
059            /**
060             * Returns the number of RRD bytes held in memory.
061             *
062             * @return Number of all RRD bytes.
063             */
064            public long getLength() {
065                    return buffer.length;
066            }
067    
068            /**
069             * Reserves a memory section as a RRD storage.
070             *
071             * @param newLength Number of bytes held in memory.
072             * @throws IOException Thrown in case of I/O error.
073             */
074            protected void setLength(long newLength) throws IOException {
075                    if (newLength > Integer.MAX_VALUE) {
076                            throw new IOException("Cannot create this big memory backed RRD");
077                    }
078                    buffer = new byte[(int) newLength];
079            }
080    
081            /**
082             * This method is required by the base class definition, but it does not
083             * releases any memory resources at all.
084             */
085            public void close() {
086                    // NOP
087            }
088    
089            /**
090             * This method is overriden to disable high-level caching in frontend JRobin classes.
091             *
092             * @return Always returns <code>false</code>. There is no need to cache anything in high-level classes
093             *         since all RRD bytes are already in memory.
094             */
095            protected boolean isCachingAllowed() {
096                    return false;
097            }
098    }