RdpCache.java :  » Net » Remote-desktop » org » rdesktop » server » rdp » Java Open Source

Java Open Source » Net » Remote desktop 
Remote desktop » org » rdesktop » server » rdp » RdpCache.java
///////////////////////////////////////////////////////////////////////////////
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License and GNU Library
//   General Public License as published by the Free Software Foundation;
//   either version 2, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//   GNU General Public License and GNU Library General Public License
//   for more details.
//
//   You should have received a copy of the GNU General Public License
//   and GNU Library General Public License along with this program; if
//   not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
//   MA 02139, USA.
//
///////////////////////////////////////////////////////////////////////////////

package org.rdesktop.server.rdp;

import java.awt.Cursor;
import java.awt.image.IndexColorModel;

public class RdpCache
{
    private static final int RDPCACHE_COLOURMAPSIZE = 0x06; // unified patch

    private RdpBitmap[][] bitmapcache = new RdpBitmap[3][600];
    private Cursor[] cursorcache = new Cursor[32];
    private RdpGlyph[][] fontcache = new RdpGlyph[12][256];
    private RdpDataBlob[] textcache = new RdpDataBlob[256];
    private int[] highdeskcache = new int[921600];
    private int num_bitmaps_in_memory[] = new int[3];
    private IndexColorModel[] colourcache = new IndexColorModel[RDPCACHE_COLOURMAPSIZE];

    public RdpCache()
    {
    }

    protected void removeLRUBitmap(int cache_id)
    {
        int i;
        int cache_idx = 0;
        int m = 0xffffffff;

        for (i = 0; i < bitmapcache[cache_id].length; i++)
        {
            if ((bitmapcache[cache_id][i] != null) && (bitmapcache[cache_id][i].getBitmapData() != null) && (bitmapcache[cache_id][i].usage < m))
            {
                cache_idx = i;
                m = bitmapcache[cache_id][i].usage;
            }
        }

        bitmapcache[cache_id][cache_idx] = null;
        --num_bitmaps_in_memory[cache_id];
    }
    
    public IndexColorModel get_colourmap(int cache_id) throws RdpDesktopException
    {
        IndexColorModel map = null;
        if (cache_id < colourcache.length)
        {
            map = colourcache[cache_id];
            if (map != null)
            {
                return map;
            }
        }

        throw new RdpDesktopException("Could not get colourmap with cache_id=" + cache_id);
    }

    public void put_colourmap(int cache_id, IndexColorModel map) throws RdpDesktopException
    {
        if (cache_id < colourcache.length)
        {
            colourcache[cache_id] = map;
        }
        else
        {
            throw new RdpDesktopException("Could not put colourmap with cache_id=" + cache_id);
        }
    }

    public RdpBitmap getBitmap(int cache_id, int cache_idx) throws RdpDesktopException
    {
        RdpBitmap bitmap = null;

        if ((cache_id < bitmapcache.length) && (cache_idx < bitmapcache[0].length))
        {
            bitmap = bitmapcache[cache_id][cache_idx];

            if (bitmap != null)
            {
                return bitmap;
            }
        }

        throw new RdpDesktopException("Could not get Bitmap!");
    }

    public void putBitmap(int cache_id, int cache_idx, RdpBitmap bitmap, int stamp) throws RdpDesktopException
    {
        if ((cache_id < bitmapcache.length) && (cache_idx < bitmapcache[0].length))
        {
            bitmapcache[cache_id][cache_idx] = bitmap;
        }
        else
        {
            throw new RdpDesktopException("Could not put Bitmap!");
        }
    }

    public Cursor getCursor(int cache_idx) throws RdpDesktopException
    {
        Cursor cursor = null;

        if (cache_idx < cursorcache.length)
        {
            cursor = cursorcache[cache_idx];
            if (cursor != null)
            {
                return cursor;
            }
        }

        throw new RdpDesktopException("Cursor not found");
    }

    public void putCursor(int cache_idx, Cursor cursor) throws RdpDesktopException
    {
        if (cache_idx < cursorcache.length)
        {
            cursorcache[cache_idx] = cursor;
        }
        else
        {
            throw new RdpDesktopException("Could not put Cursor!");
        }
    }

    public void putFont(RdpGlyph glyph) throws RdpDesktopException
    {
        if ((glyph.getFont() < fontcache.length) && (glyph.getCharacter() < fontcache[0].length))
        {
            fontcache[glyph.getFont()][glyph.getCharacter()] = glyph;
        }
        else
        {
            throw new RdpDesktopException("Could not put font");
        }
    }
    
    public RdpGlyph getFont(int font, int character) throws RdpDesktopException
    {
        if ((font < fontcache.length) && (character < fontcache[0].length))
        {
            RdpGlyph glyph = fontcache[font][character];

            if (glyph != null)
            {
                return glyph;
            }
        }

        throw new RdpDesktopException("Could not get Font:" + font + ", " + character);
    }

    public RdpDataBlob getText(int cache_id) throws RdpDesktopException
    {
        RdpDataBlob entry = null;

        if (cache_id < textcache.length)
        {
            entry = textcache[cache_id];
            if (entry != null)
            {
                if (entry.getData() != null)
                {
                    return entry;
                }
            }
        }

        throw new RdpDesktopException("Could not get Text:" + cache_id);
    }

    public void putText(int cache_id, RdpDataBlob entry) throws RdpDesktopException
    {
        if (cache_id < textcache.length)
        {
            textcache[cache_id] = entry;
        }
        else
        {
            throw new RdpDesktopException("Could not put Text");
        }
    }

    public void putDesktop(int offset, int cx, int cy, int[] data) throws RdpDesktopException
    {
        int length = cx * cy;
        int pdata = 0;

        if (offset > highdeskcache.length)
        {
            offset = 0;
        }

        if ((int) offset + length <= highdeskcache.length)
        {
            for (int i = 0; i < cy; i++)
            {
                System.arraycopy(data, pdata, highdeskcache, offset, cx);
                offset += cx;
                pdata += cx;
            }
        }
        else
        {
            throw new RdpDesktopException("Could not put Desktop");
        }
    }

    public int[] getDesktopInt(int offset, int cx, int cy) throws RdpDesktopException
    {
        int length = cx * cy;
        int pdata = 0;
        int[] data = new int[length];

        if (offset > highdeskcache.length)
        {
            offset = 0;
        }

        if ((int) offset + length <= highdeskcache.length)
        {
            for (int i = 0; i < cy; i++)
            {
                System.arraycopy(highdeskcache, offset, data, pdata, cx);
                offset += cx;
                pdata += cx;
            }

            return data;
        }

        throw new RdpDesktopException("Could not get Bitmap");
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.