Path: blob/main/src/lwjgl/java/tritonus/TCircularBuffer.java
8642 views
/*1* TCircularBuffer.java2*3* This file is part of Tritonus: http://www.tritonus.org/4*/56/*7* Copyright (c) 1999 by Matthias Pfisterer8* Copyright (c) 2012 by fireandfuel from Cuina Team (http://www.cuina.byethost12.com/)9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU Library General Public License as published12* by the Free Software Foundation; either version 2 of the License, or13* (at your option) any later version.14*15* This program is distributed in the hope that it will be useful,16* but WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18* GNU Library General Public License for more details.19*20* You should have received a copy of the GNU Library General Public21* License along with this program; if not, write to the Free Software22* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.23*24*/2526package tritonus;2728public class TCircularBuffer29{30private boolean m_bBlockingRead;31private boolean m_bBlockingWrite;32private byte[] m_abData;33private int m_nSize;34private long m_lReadPos;35private long m_lWritePos;36private Trigger m_trigger;37private boolean m_bOpen;3839public TCircularBuffer(int nSize, boolean bBlockingRead, boolean bBlockingWrite, Trigger trigger)40{41m_bBlockingRead = bBlockingRead;42m_bBlockingWrite = bBlockingWrite;43m_nSize = nSize;44m_abData = new byte[m_nSize];45m_lReadPos = 0;46m_lWritePos = 0;47m_trigger = trigger;48m_bOpen = true;49}5051public void close()52{53m_bOpen = false;54// TODO: call notify() ?55}5657private boolean isOpen()58{59return m_bOpen;60}6162public int availableRead()63{64return (int) (m_lWritePos - m_lReadPos);65}6667public int availableWrite()68{69return m_nSize - availableRead();70}7172private int getReadPos()73{74return (int) (m_lReadPos % m_nSize);75}7677private int getWritePos()78{79return (int) (m_lWritePos % m_nSize);80}8182public int read(byte[] abData)83{84return read(abData, 0, abData.length);85}8687public int read(byte[] abData, int nOffset, int nLength)88{8990if(!isOpen())91{92if(availableRead() > 0)93{94nLength = Math.min(nLength, availableRead());9596} else97{9899return -1;100}101}102synchronized(this)103{104if(m_trigger != null && availableRead() < nLength)105{106107m_trigger.execute();108}109if(!m_bBlockingRead)110{111nLength = Math.min(availableRead(), nLength);112}113int nRemainingBytes = nLength;114while(nRemainingBytes > 0)115{116while(availableRead() == 0)117{118try119{120wait();121} catch (InterruptedException e)122{123124}125}126int nAvailable = Math.min(availableRead(), nRemainingBytes);127while(nAvailable > 0)128{129int nToRead = Math.min(nAvailable, m_nSize - getReadPos());130System.arraycopy(m_abData, getReadPos(), abData, nOffset, nToRead);131m_lReadPos += nToRead;132nOffset += nToRead;133nAvailable -= nToRead;134nRemainingBytes -= nToRead;135}136notifyAll();137}138139return nLength;140}141}142143public int write(byte[] abData)144{145return write(abData, 0, abData.length);146}147148public int write(byte[] abData, int nOffset, int nLength)149{150151synchronized(this)152{153154if(!m_bBlockingWrite)155{156nLength = Math.min(availableWrite(), nLength);157}158int nRemainingBytes = nLength;159while(nRemainingBytes > 0)160{161while(availableWrite() == 0)162{163try164{165wait();166} catch (InterruptedException e)167{168169}170}171int nAvailable = Math.min(availableWrite(), nRemainingBytes);172while(nAvailable > 0)173{174int nToWrite = Math.min(nAvailable, m_nSize - getWritePos());175// TDebug.out("src buf size= " + abData.length +176// ", offset = " + nOffset + ", dst buf size=" +177// m_abData.length + " write pos=" + getWritePos() + " len="178// + nToWrite);179System.arraycopy(abData, nOffset, m_abData, getWritePos(), nToWrite);180m_lWritePos += nToWrite;181nOffset += nToWrite;182nAvailable -= nToWrite;183nRemainingBytes -= nToWrite;184}185notifyAll();186}187188return nLength;189}190}191192public static interface Trigger193{194public void execute();195}196197}198199/*** TCircularBuffer.java ***/200201202203