BinaryOutputStream.java :  » Game » l1j-tw-99nets » l1j » server » server » utils » Java Open Source

Java Open Source » Game » l1j tw 99nets 
l1j tw 99nets » l1j » server » server » utils » BinaryOutputStream.java
/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import l1j.server.Config;

public class BinaryOutputStream extends OutputStream {
  private static final String CLIENT_LANGUAGE_CODE = Config
      .CLIENT_LANGUAGE_CODE;

  private final ByteArrayOutputStream _bao = new ByteArrayOutputStream();

  public BinaryOutputStream() {
  }

  @Override
  public void write(int b) throws IOException {
    _bao.write(b);
  }

  public void writeD(int value) {
    _bao.write(value & 0xff);
    _bao.write(value >> 8 & 0xff);
    _bao.write(value >> 16 & 0xff);
    _bao.write(value >> 24 & 0xff);
  }

  public void writeH(int value) {
    _bao.write(value & 0xff);
    _bao.write(value >> 8 & 0xff);
  }

  public void writeC(int value) {
    _bao.write(value & 0xff);
  }

  public void writeP(int value) {
    _bao.write(value);
  }

  public void writeL(long value) {
    _bao.write((int) (value & 0xff));
  }

  public void writeF(double org) {
    long value = Double.doubleToRawLongBits(org);
    _bao.write((int) (value & 0xff));
    _bao.write((int) (value >> 8 & 0xff));
    _bao.write((int) (value >> 16 & 0xff));
    _bao.write((int) (value >> 24 & 0xff));
    _bao.write((int) (value >> 32 & 0xff));
    _bao.write((int) (value >> 40 & 0xff));
    _bao.write((int) (value >> 48 & 0xff));
    _bao.write((int) (value >> 56 & 0xff));
  }

  public void writeS(String text) {
    try {
      if (text != null) {
        _bao.write(text.getBytes(CLIENT_LANGUAGE_CODE));
      }
    } catch (Exception e) {
    }

    _bao.write(0);
  }

  public void writeByte(byte[] text) {
    try {
      if (text != null) {
        _bao.write(text);
      }
    } catch (Exception e) {
    }
  }

  public int getLength() {
    return _bao.size() + 2;
  }

  public byte[] getBytes() {
    return _bao.toByteArray();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.