001 /* 002 * Copyright 2009 Red Hat, Inc. 003 * Red Hat licenses this file to you under the Apache License, version 004 * 2.0 (the "License"); you may not use this file except in compliance 005 * with the License. You may obtain a copy of the License at 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * Unless required by applicable law or agreed to in writing, software 008 * distributed under the License is distributed on an "AS IS" BASIS, 009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 010 * implied. See the License for the specific language governing 011 * permissions and limitations under the License. 012 */ 013 014 package org.hornetq.api.core; 015 016 import java.nio.ByteBuffer; 017 018 import org.hornetq.core.buffers.impl.ChannelBufferWrapper; 019 import org.jboss.netty.buffer.ChannelBuffer; 020 import org.jboss.netty.buffer.ChannelBuffers; 021 022 /** 023 * Factory class to create HornetQBuffers 024 * 025 * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a> 026 */ 027 public class HornetQBuffers 028 { 029 /** 030 * Creates a <em>self-expanding</em> HornetQBuffer with the given initial size 031 * 032 * @param size the initial size of the created HornetQBuffer 033 * @return a self-expanding HornetQBuffer starting with the given size 034 */ 035 public static HornetQBuffer dynamicBuffer(final int size) 036 { 037 return new ChannelBufferWrapper(ChannelBuffers.dynamicBuffer(size)); 038 } 039 040 /** 041 * Creates a <em>self-expanding</em> HornetQBuffer filled with the given byte array 042 * 043 * @param bytes the created buffer will be initially filled with this byte array 044 * @return a self-expanding HornetQBuffer filled with the given byte array 045 */ 046 public static HornetQBuffer dynamicBuffer(final byte[] bytes) 047 { 048 ChannelBuffer buff = ChannelBuffers.dynamicBuffer(bytes.length); 049 050 buff.writeBytes(bytes); 051 052 return new ChannelBufferWrapper(buff); 053 } 054 055 /** 056 * Creates a HornetQBuffer wrapping an underlying NIO ByteBuffer 057 * 058 * The position on this buffer won't affect the position on the inner buffer 059 * 060 * @param underlying the underlying NIO ByteBuffer 061 * @return a HornetQBuffer wrapping the underlying NIO ByteBuffer 062 */ 063 public static HornetQBuffer wrappedBuffer(final ByteBuffer underlying) 064 { 065 HornetQBuffer buff = new ChannelBufferWrapper(ChannelBuffers.wrappedBuffer(underlying)); 066 067 buff.clear(); 068 069 return buff; 070 } 071 072 /** 073 * Creates a HornetQBuffer wrapping an underlying byte array 074 * 075 * @param underlying the underlying byte array 076 * @return a HornetQBuffer wrapping the underlying byte array 077 */ 078 public static HornetQBuffer wrappedBuffer(final byte[] underlying) 079 { 080 return new ChannelBufferWrapper(ChannelBuffers.wrappedBuffer(underlying)); 081 } 082 083 /** 084 * Creates a <em>fixed</em> HornetQBuffer of the given size 085 * 086 * @param size the size of the created HornetQBuffer 087 * @return a fixed HornetQBuffer with the given size 088 */ 089 public static HornetQBuffer fixedBuffer(final int size) 090 { 091 return new ChannelBufferWrapper(ChannelBuffers.buffer(size)); 092 } 093 094 private HornetQBuffers() 095 { 096 } 097 }