0N/A/*
5399N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.management.jdp;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.DataOutputStream;
6338N/Aimport java.io.IOException;
6338N/A
0N/A/**
0N/A * JdpPacketWriter responsible for writing a packet
0N/A * <p>This class assembles a set of key/value pairs to valid JDP packet,
0N/A * ready to be sent across a Net</p>
0N/A */
0N/Apublic final class JdpPacketWriter {
0N/A
0N/A private final ByteArrayOutputStream baos;
0N/A private final DataOutputStream pkt;
0N/A
0N/A /**
0N/A * Create a JDP packet, add mandatory magic and version headers
0N/A *
0N/A * @throws IOException
0N/A */
0N/A public JdpPacketWriter()
0N/A throws IOException {
0N/A baos = new ByteArrayOutputStream();
0N/A pkt = new DataOutputStream(baos);
0N/A
0N/A pkt.writeInt(JdpGenericPacket.getMagic());
0N/A pkt.writeShort(JdpGenericPacket.getVersion());
0N/A }
0N/A
0N/A /**
0N/A * Put string entry to packet
0N/A *
0N/A * @param entry - string to put (utf-8 encoded)
0N/A * @throws IOException
0N/A */
0N/A public void addEntry(String entry)
0N/A throws IOException {
0N/A /* DataOutputStream.writeUTF() do essentially
0N/A * the same as:
0N/A * pkt.writeShort(entry.getBytes("UTF-8").length);
0N/A * pkt.write(entry.getBytes("UTF-8"));
0N/A */
0N/A pkt.writeUTF(entry);
0N/A }
0N/A
0N/A /**
0N/A * Put key/value pair to packet
0N/A *
0N/A * @param key - key to put (utf-8 encoded)
0N/A * @param val - value to put (utf-8 encoded)
0N/A * @throws IOException
0N/A */
0N/A public void addEntry(String key, String val)
0N/A throws IOException {
0N/A /* Silently skip key if value is null.
0N/A * We don't need to distinguish between key missing
0N/A * and key has no value cases
0N/A */
0N/A if (val != null) {
0N/A addEntry(key);
0N/A addEntry(val);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return assembled packet as a byte array
0N/A *
0N/A * @return packet bytes
0N/A */
0N/A public byte[] getPacketBytes() {
0N/A return baos.toByteArray();
0N/A }
0N/A}
0N/A