001 package net.minecraft.network.packet;
002
003 import java.io.DataInputStream;
004 import java.io.DataOutputStream;
005 import java.io.IOException;
006
007 public class Packet250CustomPayload extends Packet
008 {
009 /** Name of the 'channel' used to send data */
010 public String channel;
011
012 /** Length of the data to be read */
013 public int length;
014
015 /** Any data */
016 public byte[] data;
017
018 public Packet250CustomPayload() {}
019
020 public Packet250CustomPayload(String par1Str, byte[] par2ArrayOfByte)
021 {
022 this.channel = par1Str;
023 this.data = par2ArrayOfByte;
024
025 if (par2ArrayOfByte != null)
026 {
027 this.length = par2ArrayOfByte.length;
028
029 if (this.length > 32767)
030 {
031 throw new IllegalArgumentException("Payload may not be larger than 32k");
032 }
033 }
034 }
035
036 /**
037 * Abstract. Reads the raw packet data from the data stream.
038 */
039 public void readPacketData(DataInputStream par1DataInputStream) throws IOException
040 {
041 this.channel = readString(par1DataInputStream, 20);
042 this.length = par1DataInputStream.readShort();
043
044 if (this.length > 0 && this.length < 32767)
045 {
046 this.data = new byte[this.length];
047 par1DataInputStream.readFully(this.data);
048 }
049 }
050
051 /**
052 * Abstract. Writes the raw packet data to the data stream.
053 */
054 public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
055 {
056 writeString(this.channel, par1DataOutputStream);
057 par1DataOutputStream.writeShort((short)this.length);
058
059 if (this.data != null)
060 {
061 par1DataOutputStream.write(this.data);
062 }
063 }
064
065 /**
066 * Passes this Packet on to the NetHandler for processing.
067 */
068 public void processPacket(NetHandler par1NetHandler)
069 {
070 par1NetHandler.handleCustomPayload(this);
071 }
072
073 /**
074 * Abstract. Return the size of the packet (not counting the header).
075 */
076 public int getPacketSize()
077 {
078 return 2 + this.channel.length() * 2 + 2 + this.length;
079 }
080 }