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 used to represent the end of a stream. 27 * 28 * @version $Revision$ 29 */ 30 final public class EOSPacket implements Packet { 31 32 static final public EOSPacket EOS_PACKET = new EOSPacket(); 33 34 private EOSPacket() { 35 } 36 37 public void writeTo(OutputStream out) throws IOException { 38 } 39 public void writeTo(DataOutput out) throws IOException { 40 } 41 42 public int position() { 43 return 1; 44 } 45 46 public void position(int position) { 47 } 48 49 public int limit() { 50 return 0; 51 } 52 53 public void limit(int limit) { 54 } 55 56 public void flip() { 57 } 58 59 public int remaining() { 60 return -1; 61 } 62 63 public void rewind() { 64 } 65 66 public boolean hasRemaining() { 67 return false; 68 } 69 70 public void clear() { 71 } 72 73 public int capacity() { 74 return 0; 75 } 76 77 public Packet slice() { 78 return this; 79 } 80 81 public Packet duplicate() { 82 return this; 83 } 84 85 public Object duplicate(ClassLoader cl) throws IOException { 86 try { 87 Class clazz = cl.loadClass(EOSPacket.class.getName()); 88 return clazz.getField("EOS_PACKET").get(null); 89 } catch (Throwable e) { 90 throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e); 91 } 92 } 93 94 /*** 95 * @see org.activeio.Packet#read() 96 */ 97 public int read() { 98 return -1; 99 } 100 101 /*** 102 * @see org.activeio.Packet#read(byte[], int, int) 103 */ 104 public int read(byte[] data, int offset, int length) { 105 return -1; 106 } 107 108 /*** 109 * @see org.activeio.Packet#write(int) 110 */ 111 public boolean write(int data) { 112 return false; 113 } 114 115 /*** 116 * @see org.activeio.Packet#write(byte[], int, int) 117 */ 118 public int write(byte[] data, int offset, int length) { 119 return -1; 120 } 121 122 public ByteSequence asByteSequence() { 123 return EmptyPacket.EMPTY_BYTE_SEQUENCE; 124 } 125 126 public byte[] sliceAsBytes() { 127 return EmptyPacket.EMPTY_BYTE_ARRAY; 128 } 129 130 /*** 131 * @param dest 132 * @return the number of bytes read into the dest. 133 */ 134 public int read(Packet dest) { 135 return 0; 136 } 137 138 public String toString() { 139 return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}"; 140 } 141 142 public Object narrow(Class target) { 143 if( target.isAssignableFrom(getClass()) ) { 144 return this; 145 } 146 return null; 147 } 148 149 public void dispose() { 150 } 151 152 }