/// <summary> /// Write packet header into /// <paramref name="pkt"/> /// , /// return the length of the header written. /// </summary> private int WritePacketHeader(ByteBuffer pkt, int dataLen, int packetLen) { pkt.Clear(); // both syncBlock and syncPacket are false PacketHeader header = new PacketHeader(packetLen, offset, seqno, (dataLen == 0), dataLen, false); int size = header.GetSerializedSize(); pkt.Position(PacketHeader.PktMaxHeaderLen - size); header.PutInBuffer(pkt); return(size); }
/// <summary>Write the full packet, including the header, to the given output stream. /// </summary> /// <param name="stm"/> /// <exception cref="System.IO.IOException"/> internal virtual void WriteTo(DataOutputStream stm) { lock (this) { CheckBuffer(); int dataLen = dataPos - dataStart; int checksumLen = checksumPos - checksumStart; int pktLen = HdfsConstants.BytesInInteger + dataLen + checksumLen; PacketHeader header = new PacketHeader(pktLen, offsetInBlock, seqno, lastPacketInBlock , dataLen, syncBlock); if (checksumPos != dataStart) { // Move the checksum to cover the gap. This can happen for the last // packet or during an hflush/hsync call. System.Array.Copy(buf, checksumStart, buf, dataStart - checksumLen, checksumLen); checksumPos = dataStart; checksumStart = checksumPos - checksumLen; } int headerStart = checksumStart - header.GetSerializedSize(); System.Diagnostics.Debug.Assert(checksumStart + 1 >= header.GetSerializedSize()); System.Diagnostics.Debug.Assert(headerStart >= 0); System.Diagnostics.Debug.Assert(headerStart + header.GetSerializedSize() == checksumStart ); // Copy the header data into the buffer immediately preceding the checksum // data. System.Array.Copy(header.GetBytes(), 0, buf, headerStart, header.GetSerializedSize ()); // corrupt the data for testing. if (DFSClientFaultInjector.Get().CorruptPacket()) { buf[headerStart + header.GetSerializedSize() + checksumLen + dataLen - 1] ^= unchecked ( (int)(0xff)); } // Write the now contiguous full packet to the output stream. stm.Write(buf, headerStart, header.GetSerializedSize() + checksumLen + dataLen); // undo corruption. if (DFSClientFaultInjector.Get().UncorruptPacket()) { buf[headerStart + header.GetSerializedSize() + checksumLen + dataLen - 1] ^= unchecked ( (int)(0xff)); } } }