1 /*** 2 * 3 * Copyright 2004 Hiram Chirino 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 * use this file except in compliance with the License. You may obtain a copy of 7 * the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 package org.activeio.packet; 18 19 import java.io.DataOutput; 20 import java.io.IOException; 21 import java.io.OutputStream; 22 23 import org.activeio.Packet; 24 25 /*** 26 * Provides a Packet implementation that is directly backed by a <code>byte[0]</code>. 27 * 28 * @version $Revision$ 29 */ 30 final public class EmptyPacket implements Packet { 31 32 static final public EmptyPacket EMPTY_PACKET = new EmptyPacket(); 33 static final byte EMPTY_BYTE_ARRAY[] = new byte[]{}; 34 static final ByteSequence EMPTY_BYTE_SEQUENCE = new ByteSequence(EMPTY_BYTE_ARRAY,0,0); 35 36 private EmptyPacket() { 37 } 38 39 public void writeTo(OutputStream out) throws IOException { 40 } 41 public void writeTo(DataOutput out) throws IOException { 42 } 43 44 public int position() { 45 return 0; 46 } 47 48 public void position(int position) { 49 } 50 51 public int limit() { 52 return 0; 53 } 54 55 public void limit(int limit) { 56 } 57 58 public void flip() { 59 } 60 61 public int remaining() { 62 return 0; 63 } 64 65 public void rewind() { 66 } 67 68 public boolean hasRemaining() { 69 return false; 70 } 71 72 public void clear() { 73 } 74 75 public int capacity() { 76 return 0; 77 } 78 79 public Packet slice() { 80 return this; 81 } 82 83 public Packet duplicate() { 84 return this; 85 } 86 87 public Object duplicate(ClassLoader cl) throws IOException { 88 try { 89 Class clazz = cl.loadClass(EmptyPacket.class.getName()); 90 return clazz.getField("EMPTY_PACKET").get(null); 91 } catch (Throwable e) { 92 throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e); 93 } 94 } 95 96 /*** 97 * @see org.activeio.Packet#read() 98 */ 99 public int read() { 100 return -1; 101 } 102 103 /*** 104 * @see org.activeio.Packet#read(byte[], int, int) 105 */ 106 public int read(byte[] data, int offset, int length) { 107 return -1; 108 } 109 110 /*** 111 * @see org.activeio.Packet#write(int) 112 */ 113 public boolean write(int data) { 114 return false; 115 } 116 117 /*** 118 * @see org.activeio.Packet#write(byte[], int, int) 119 */ 120 public int write(byte[] data, int offset, int length) { 121 return -1; 122 } 123 124 public ByteSequence asByteSequence() { 125 return EMPTY_BYTE_SEQUENCE; 126 } 127 128 public byte[] sliceAsBytes() { 129 return EMPTY_BYTE_ARRAY; 130 } 131 132 /*** 133 * @param dest 134 * @return the number of bytes read into the dest. 135 */ 136 public int read(Packet dest) { 137 return -1; 138 } 139 140 public String toString() { 141 return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}"; 142 } 143 144 public Object narrow(Class target) { 145 if( target.isAssignableFrom(getClass()) ) { 146 return this; 147 } 148 return null; 149 } 150 151 public void dispose() { 152 } 153 154 }