Android Open Source - TreeFrogEngine Writer






From Project

Back to project page TreeFrogEngine.

License

The source code is released under:

MIT License

If you think the Android project TreeFrogEngine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.discretesoftworks.networking;
//from   w  w  w. java 2  s.  com
/*
Custom Class by Preston Turner for reading and writing information to a stream.

  Methods:
    setStreams(out,in) // Must be called before anything else
    sendmessage()
    writebyte(int)
    writeshort(int)
    writeushort(int)
    writestring(String)
    readbyte()
    readshort()
    readushort()
    readstring()

*/

import java.io.*;

public class Writer{
  
  private BufferedInputStream in;
  private BufferedOutputStream out;
  
  public final int MSG_PLAYER = 1;
  public final int MSG_INFO = 2;
  public final int MSG_MOVEPLAYER = 3;
  public final int MSG_JUMP = 4;
  public final int MSG_DIR = 5;
  public final int MSG_FIRING = 6;
  public final int MSG_CLOSE = 7;
  public final int MSG_PLAYERDOWN = 8;
  public final int MSG_HIT = 9;
  public final int MSG_KILL = 10;
  public final int MSG_SCORES = 11;
  
  public Writer(){}

  /* MUST be called before any other methods!! */
  public void setStreams(BufferedOutputStream out, BufferedInputStream in){
    this.in = in;
    this.out = out;
  }
  
  /* Clears the buffer (flushes - not needed) */
  public void clearbuffer() throws IOException{
    out.flush();
  }
  
  /* Sends the buffered message */
  public void sendmessage() throws IOException{
    out.flush();
  }
  
  /* Reads a 1-byte integer (0-255) */
  public int readbyte() throws IOException{
    int num = 0;
    if (in.available() > 0)
      num = in.read();
    else
      System.err.println("Invalid reciept");
    return num;
  }
  
  /* Writes a 1-byte integer (0-255) */
  public void writebyte(int num) throws IOException{
    out.write(num);
  }
  
  /* Reads a 2-byte signed integer */
  public int readshort() throws IOException{
    int num = 0;
    if (in.available() >= 2){
      num = (readbyte()*255);
      num += readbyte();
      num -= (int)(65536/2);
    }
    else{
      System.err.println("Invalid reciept");
    }
    return num;
  }
  
  /* Writes a 2-byte signed integer */
  public void writeshort(int num) throws IOException{
    num += (int)(65536/2);
    writebyte((int)(num/255));
    writebyte(num%255);
  }
  
  
  
  /* Reads a 2-byte UNSIGNED integer */
  public int readushort() throws IOException{
    int num = 0;
    if (in.available() >= 2){
      num = (in.read()*255);
      num += in.read();
    }
    else{
      System.err.println("Invalid reciept");
    }
    return num;
  }
  
  /* Writes a 2-byte UNSIGNED integer */
  public void writeushort(int num) throws IOException{
    out.write((int)(num/255));
    out.write(num%255);
  }
  
  /* Reads a 4-byte UNSIGNED integer */
  public int readuint() throws IOException{
    int num = 0;
    if (in.available() >= 4){
      num = (in.read()*255);
      num = (in.read()*255);
      num = (in.read()*255);
      num += in.read();
    }
    else{
      System.err.println("Invalid reciept");
    }
    return num;
  }
  
  /* Writes a 4-byte UNSIGNED integer */
  public void writeuint(int num) throws IOException{
    out.write((int)(num/=255));
    out.write((int)(num/=255));
    out.write((int)(num/=255));
    out.write(num%255);
  }
  
  /* Reads a String */
  public String readstring() throws IOException{
    String string = "";
    int len = readbyte();
    for (int i = 0; i<len; i++){
      int b = readbyte();
      string += ""+Character.toChars(b)[0];
    }
    return string;
  }
  
  /* Writes a String */
  public void writestring(String string) throws IOException{
    writebyte(string.length());
    byte[] array = string.getBytes();
    for (int i : array){
      writebyte(i);
    }
  }
  
  /* Reads a boolean */
  public boolean readboolean() throws IOException{
    if (in.available() > 0)
      return (readbyte() == 1);
    else{
      System.err.println("Invalid reciept");
      return false;
    }
  }
  
  /* Writes a boolean */
  public void writeboolean(boolean bool) throws IOException{
    if (bool)
      writebyte(1);
    else
      writebyte(0);
  }
  
  /* Returns the input Stream */
  public InputStream getInputStream(){
    return in;
  }
  
  /* Returns the output Stream */
  public OutputStream getOutputStream(){
    return out;
  }
}




Java Source Code List

com.discretesoftworks.OUYAframework.OuyaGameController.java
com.discretesoftworks.OUYAframework.OuyaGame.java
com.discretesoftworks.TestGame.Floor.java
com.discretesoftworks.TestGame.Player.java
com.discretesoftworks.TestGame.Pointer.java
com.discretesoftworks.TestGame.TestController.java
com.discretesoftworks.TestGame.TestGame.java
com.discretesoftworks.framework.AndroidGame.java
com.discretesoftworks.framework.Assets.java
com.discretesoftworks.framework.Directional.java
com.discretesoftworks.framework.GameController.java
com.discretesoftworks.framework.GameFont.java
com.discretesoftworks.framework.GameObject.java
com.discretesoftworks.framework.GameRenderer.java
com.discretesoftworks.framework.GriddedObject.java
com.discretesoftworks.framework.ModelLoader.java
com.discretesoftworks.framework.MovingObject.java
com.discretesoftworks.framework.MyGLRenderer.java
com.discretesoftworks.framework.MyGLSurfaceView.java
com.discretesoftworks.framework.NumericalMatrix.java
com.discretesoftworks.framework.Pair.java
com.discretesoftworks.framework.Path.java
com.discretesoftworks.framework.Pool.java
com.discretesoftworks.framework.RaggedDictionary.java
com.discretesoftworks.framework.RenderModel.java
com.discretesoftworks.framework.Sprite.java
com.discretesoftworks.framework.Terrain.java
com.discretesoftworks.framework.TextDisplay.java
com.discretesoftworks.framework.TextHolder.java
com.discretesoftworks.framework.View.java
com.discretesoftworks.networking.NetworkGameController.java
com.discretesoftworks.networking.Writer.java