Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
The MIT License Copyright (c) 2010 Tad Glines Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 package com.glines.socketio.server.transport;
 
 import java.util.List;
 
 public class TransportBuffer {
 	public interface BufferListener {

Parameters:
message
Returns:
false if message send timed-out or failed
 
 		boolean onMessage(String message);
 		boolean onMessages(List<Stringmessages);
 	}
 	
 	private final int bufferSize;
 	private final Semaphore inputSemaphore;
 	private final BlockingQueue<Stringqueue = new LinkedBlockingQueue<String>();
 	
 	public TransportBuffer(int bufferSize) {
 		this. = bufferSize;
 		this. = new Semaphore(bufferSize);
 	}
 
 	public void setListener(BufferListener listener) {
 		this..set(listener);
 	}
 	
 	public int getBufferSize() {
 		return ;
 	}
 
 	public int getAvailableBytes() {
 	}
 
 	public int getFreeBytes() {
 	}
 
 	public boolean isEmpty() {
 		return .isEmpty();
 	}
 
 	public void clear() {
 		List<Stringlist = new ArrayList<String>();
 		.drainTo(list);
 
 		for (String strlist) {
 		}
 	}
 	
 	public List<StringdrainMessages() {
 		List<Stringlist = new ArrayList<String>();
 		.drainTo(list);
 
 		for (String strlist) {
 		}
 		
 		return list;
 	}
 	
 	public String getMessage(long timeout) {
 		try {
 			String msg = .poll(timeout.);
 			if (msg != null) {
 			}
 			return msg;
catch (InterruptedException e) {
			return null;
		}
	}
	public boolean putMessage(String messagelong timeout) {
		if (listener != null) {
			try {
				if (.size() == 0) {
					return listener.onMessage(message);
else {
					ArrayList<Stringmessages = new ArrayList<String>(.size()+1);
					.drainTo(messages);
					messages.add(message);
					return listener.onMessages(messages);
				}
catch (Throwable t) {
				return false;
			}
else {
			try {
				if (!.tryAcquire(message.length(), timeout.)) {
					return false;
				}
				.offer(message);
				return true;
catch (InterruptedException e) {
				return false;
			}
		}
	}
New to GrepCode? Check out our FAQ X