Package de.desy.tine.structUtils

Source Code of de.desy.tine.structUtils.TStructIo

/*
* Created on Feb 23, 2005
*
*/
package de.desy.tine.structUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import de.desy.tine.io.TDataInputStream;
import de.desy.tine.io.TDataOutputStream;

/**
* \internal
*
* @author jwlg
*/
public class TStructIo
{
  /**
   * Disable constructor
   */
  private TStructIo()
  {
  }
  /**
   * Lets a structure read from a byte array.
   *
   * @param struct
   * @param b
   * @throws IOException
   */
  public static void bytesToStruct(TStructIfc struct, byte[] b) throws IOException
  {
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(b);
    TDataInputStream istream = new TDataInputStream(bytesIn);
    struct.readData(istream);
    istream.close();
  }
  /**
   * Lets a structure read from a byte array.
   *
   * @param struct
   *          An object which implements TStructIfc.
   * @param b
   *          A byte array.
   * @param offset
   *          Offset within byte array.
   * @param len
   *          Max. lenth of bytes.
   * @throws IOException
   */
  public static void bytesToStruct(TStructIfc struct, byte[] b, int offset, int len) throws IOException
  {
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(b, offset, len);
    TDataInputStream istream = new TDataInputStream(bytesIn);
    struct.readData(istream);
    istream.close();
  }
  /**
   * Lets a structure write its contents to a byte array.
   *
   * @param struct
   * @return
   * @throws IOException
   */
  public static byte[] structToBytes(TStructIfc struct) throws IOException
  {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    TDataOutputStream ostream = new TDataOutputStream(bytesOut);
    struct.writeData(ostream);
    ostream.close();
    return bytesOut.toByteArray();
  }
}
TOP

Related Classes of de.desy.tine.structUtils.TStructIo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.