Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sharing my ByteStream, feel free to improve!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Sharing my ByteStream, feel free to improve!

    Hi guys,

    I wrote this a few days ago for my MMORPG.
    You can read and write Strings, etc in a Input- or OutputStream with bytes.
    package com.verficon.io;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    public class ByteStream {
    	private byte[] buffer;
    	private int pos;
     
    	public static ByteStream create() {
    		final ByteStream s = new ByteStream();
    		s.buffer = new byte[1024];
    		return s;
    	}
     
    	public boolean readBuffer(final InputStream is) throws IOException {
    		return is.read(buffer) != -1;
    	}
     
    	public byte readByte() {
    		checkBuffer();
    		return buffer[pos++];
    	}
     
    	public void writeByte(final byte b) {
    		checkBuffer();
    		buffer[pos++] = b;
    	}
     
    	private void checkBuffer() {
    		if (pos >= buffer.length)
    			pos = 0;
    	}
     
    	public String readString() {
    		final byte length = readByte();
    		final byte[] b = new byte[length];
    		for (int i = 0; i < length; i++) {
    			b[i] = readByte();
    		}
    		return new String(b);
    	}
     
    	public void writeString(final String s) {
    		final byte[] b = s.getBytes();
    		writeByte((byte) b.length);
    		for (int i = 0; i < b.length; i++) {
    			writeByte(b[i]);
    		}
    	}
     
    	public void flushBuffer(final OutputStream os) throws IOException {
    		os.write(buffer);
    		buffer = new byte[1024];
    	}
    }
    Have fun, feel free to add something!
    Feel free to click the 'Thank' button

    Download as External Java Libary: http://www.multiupload.com/AIRXTDUHFZ
    Last edited by Verficon; March 6th, 2011 at 05:33 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Sharing my ByteStream, feel free to improve!

    Hello Verficon.

    It would be good if you could comment this and write up exactly what it does.
    Then you could submit it to: Java Code Snippets and Tutorials
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Feel like I'm overthinking this problem.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2011, 08:02 AM
  2. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  3. Look & Feel
    By Asido in forum AWT / Java Swing
    Replies: 3
    Last Post: September 10th, 2010, 09:13 PM
  4. Critique Java Game: Help Me Improve
    By gretty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 21st, 2010, 07:49 PM
  5. Replies: 2
    Last Post: October 14th, 2009, 10:10 AM