Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm looking for a hash function that: Hashes textual strings well (e.g. few collisions) Is written in Java, and widely used Bonus: works on several fields (instead of me concatenating them and applying the hash on the concatenated string) Bonus: Has a 128-bit variant. Bonus: Not CPU intensive.
I was writing an article in which I wanted to show how to send objects across the network, from Java to CLR/.Net and Back. http://ferozedaud.blogspot.com/2009/11/howto-serialize-data-from-object-from.html While doing research for this, I could not find a Java equivalent for BitConverter class that exists in .NET. Due to this, I had to resort to using ByteBuffer to marshal primitive types to b...
I have numbers written as ASCII codes in my file. For example "9" is stored as two bytes 57 i.e. 8 bits in total. I want to optimize storage by just storing those numbers as binary values for example numbers from 0-9 to be stored using 4 bits only. Any help?!
I have written a .obj parser in java to modelize 3D objects on iPhone. I would like to export the data as a binary file, which must be as small as possible. I have plenty of indices that would fit a unsigned short, but they are represented as int in java. I would like to use the ByteBuffer class to do the conversion just before writing the data in a file. I suppose I will have to manipulate b...
How do I convert a long to a byte[] and back in Java? I'm trying convert a long to a byte[] so that I will be able to send the byte[] over a tcp connection. On the other side I want to take that byte[] and convert it back into a double. Any tips would be appreciated.
I want to change a values in byte array to put a long timestamp value in in the MSBs. Can someone tell me whats the best way to do it. I do not want to insert values bit-by-bit which I believe is very inefficient. long time = System.currentTimeMillis(); Long timeStamp = new Long(time); byte[] bArray = new byte[128]; What I want is something like: byte[0-63] = timeStamp.byteValue(); Is s...
When sending information from a java application to a C# application through sockets, is the byte order different? Or can I just send an integer from C# to a java application and read it as integer? (And do the OS matter, or is the same for java/.net no matter how the actual OS handles it?)
Lazy programmer alert. :) Cassandra stores column values as bytes (Java example). Specifying a LongType comparator compares those bytes as a long. I want the value of a long into a Cassandra-friendly byte[]. How? I poked around for awhile. I think you people can help me faster. EDIT: Both Alexander and Eli's answers agreed with this reverse transformation. Thanks!
I need to send a Network Order short for a game server I'm writing using Java. I read about network order, but I couldn't find any details about a short that is sent before the data. Could someone explain to me what it is, and how to send one to a client with Java?
If I have a file, and I want to literally write '42' to it (the value, not the string), which for example is 2a in hex, how do I do it? I want to be able to use something like outfile.write(42) or outfile.write(2a) and not write the string to the file. (I realize this is a simple question but I can't find the answer of google, probably because I don't know the correct search terms)
I have an object of type X which I want to convert into byte array before sending it to store in S3. Can anybody tell me how to do this? I appreciate your help.
Okay, so I'm working on a project where I use a Java program to initiate a socket connection between two classes (a FileSender and FileReceiver). My basic idea was that the FileSender would look like this: try { writer = new DataOutputStream(connect.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //While we have bytes to send whi...
I have a couple of questions regarding the Bittorrent Peer Wire Protocol. I am trying to implement it in Java using this spec. In Peer Wire Protocol section it says that all integers are four byte big endian values. AFAIK java uses big endian. Does that mean say if i want to send a choke message choke:<len=0001><id=0> Do i just write to the sokcet 1 followed by 0? As for my seco...
What is the best possible way to send an int through a socket in Java? Right now I'm looking at sockout.write((byte)( length >> 24 )); sockout.write((byte)( (length << 8) >> 24 )); sockout.write((byte)( (length << 16) >> 24 )); sockout.write((byte)( (length << 24) >> 24 )); and then trying to rebuild the int from bytes on the other side, but it does...
I am designing an archive format(Just for fun) in Java using this template- First 4 bytes: Number of files in the archive Next 4 bytes: Number of bytes in the filename Next N bytes: Filename Next 10 bytes: Number of bytes in the file Next N bytes: File contents from PHP Safe way to download mutliple files and save them. I am having on trouble with finding the values of the number of files e...
Is there a function for FILE (fopen?) that allows me to just read one int from a binary file? So far I'm trying this, but I'm getting some kind of error I can't see cause the program just crashes without telling me. void opentest() { FILE *fp = fopen("dqmapt.mp", "r"); int i = 0; int j = 0; int k = 0; int * buffer; if (fp) { buffer = (int *) (sizeof(int)); ...
I want to convert the attributes of my 'person' object to a representation where I can write lines to a file representing each person. A number of machines need to concurrently read their allocated chunk of the file and so I am moving from lines of character encoded text to list of bytes instead. I hope that by each person's representation being the same length I don't have to read every charac...
I am attempting to have my android phone connect to my servlet and send it a certain image. The way I figured I would do this, is to use the copyPixelsToBuffer() function and then attempt to send this to the servlet through some output stream(similar to how I would do it in a normal stand alone java application). Will this way work? If so, what kind of stream do I use exactly? Should I just use...
I'm making use of BitSet in Java to store values I have to serialize and deserialize into/from lower abstraction levels. This is to create a file-format container. Currently I want to store a String into a BitSet: private BitSet string_to_bit(String content_string) { BitSet result = new BitSet(this.size); char[] content_string_arr = new char[content_string.length()]; ...
I'm implementing a server push via a socket connection to an Objective-C based application. I'm at the point where I'm sending my data through an outputstream on the socket but I need to reassemble the data on the Objective-c end. At this point I haven't even looked at the objective-c IO classes and I am wondering how can I ensure maximum compatibility on the java end? I'm guessing I have to st...
I'm trying to write data to a file in binary format for compression. The data consists entirely of floating points so I decided to quantize the data to an intergers between 0 and 65535 so the data can be written as two bit unsigned integers and ultimately save space. However, I need to output that quantized data to a file in binary instead of human-readable Ascii. At the moment this is what I'...
  /*
   * Copyright 1994-2004 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.io;

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

Author(s):
unascribed
Since:
JDK1.0
See also:
DataInputStream
 
 public
 class DataOutputStream extends FilterOutputStream implements DataOutput {
    
The number of bytes written to the data output stream so far. If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
 
     protected int written;

    
bytearr is initialized on demand by writeUTF
 
     private byte[] bytearr = null;

    
Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.

Parameters:
out the underlying output stream, to be saved for later use.
See also:
FilterOutputStream.out
 
     public DataOutputStream(OutputStream out) {
         super(out);
     }

    
Increases the written counter by the specified value until it reaches Integer.MAX_VALUE.
 
     private void incCount(int value) {
         int temp =  + value;
         if (temp < 0) {
             temp = .;
         }
          = temp;
     }

    
Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. If no exception is thrown, the counter written is incremented by 1.

Implements the write method of OutputStream.

Parameters:
b the byte to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
 
     public synchronized void write(int bthrows IOException {
         .write(b);
         incCount(1);
     }

    
Writes len bytes from the specified byte array starting at offset off to the underlying output stream. If no exception is thrown, the counter written is incremented by len.

Parameters:
b the data.
off the start offset in the data.
len the number of bytes to write.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public synchronized void write(byte b[], int offint len)
        throws IOException
    {
        .write(bofflen);
        incCount(len);
    }

    
Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

The flush method of DataOutputStream calls the flush method of its underlying output stream.

Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
OutputStream.flush()
    public void flush() throws IOException {
        .flush();
    }

    
Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. If no exception is thrown, the counter written is incremented by 1.

Parameters:
v a boolean value to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeBoolean(boolean vthrows IOException {
        .write(v ? 1 : 0);
        incCount(1);
    }

    
Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.

Parameters:
v a byte value to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeByte(int vthrows IOException {
        .write(v);
        incCount(1);
    }

    
Writes a short to the underlying output stream as two bytes, high byte first. If no exception is thrown, the counter written is incremented by 2.

Parameters:
v a short to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeShort(int vthrows IOException {
        .write((v >>> 8) & 0xFF);
        .write((v >>> 0) & 0xFF);
        incCount(2);
    }

    
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Parameters:
v a char value to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeChar(int vthrows IOException {
        .write((v >>> 8) & 0xFF);
        .write((v >>> 0) & 0xFF);
        incCount(2);
    }

    
Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

Parameters:
v an int to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeInt(int vthrows IOException {
        .write((v >>> 24) & 0xFF);
        .write((v >>> 16) & 0xFF);
        .write((v >>>  8) & 0xFF);
        .write((v >>>  0) & 0xFF);
        incCount(4);
    }
    private byte writeBuffer[] = new byte[8];

    
Writes a long to the underlying output stream as eight bytes, high byte first. In no exception is thrown, the counter written is incremented by 8.

Parameters:
v a long to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeLong(long vthrows IOException {
        [0] = (byte)(v >>> 56);
        [1] = (byte)(v >>> 48);
        [2] = (byte)(v >>> 40);
        [3] = (byte)(v >>> 32);
        [4] = (byte)(v >>> 24);
        [5] = (byte)(v >>> 16);
        [6] = (byte)(v >>>  8);
        [7] = (byte)(v >>>  0);
        .write(, 0, 8);
        incCount(8);
    }

    
Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. If no exception is thrown, the counter written is incremented by 4.

Parameters:
v a float value to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
java.lang.Float.floatToIntBits(float)
    public final void writeFloat(float vthrows IOException {
        writeInt(Float.floatToIntBits(v));
    }

    
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. If no exception is thrown, the counter written is incremented by 8.

Parameters:
v a double value to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
java.lang.Double.doubleToLongBits(double)
    public final void writeDouble(double vthrows IOException {
        writeLong(Double.doubleToLongBits(v));
    }

    
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. If no exception is thrown, the counter written is incremented by the length of s.

Parameters:
s a string of bytes to be written.
Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public final void writeBytes(String sthrows IOException {
        int len = s.length();
        for (int i = 0 ; i < len ; i++) {
            .write((byte)s.charAt(i));
        }
        incCount(len);
    }

    
Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by the writeChar method. If no exception is thrown, the counter written is incremented by twice the length of s.

Parameters:
s a String value to be written.
Throws:
IOException if an I/O error occurs.
See also:
writeChar(int)
FilterOutputStream.out
    public final void writeChars(String sthrows IOException {
        int len = s.length();
        for (int i = 0 ; i < len ; i++) {
            int v = s.charAt(i);
            .write((v >>> 8) & 0xFF);
            .write((v >>> 0) & 0xFF);
        }
        incCount(len * 2);
    }

    
Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

Parameters:
str a string to be written.
Throws:
IOException if an I/O error occurs.
    public final void writeUTF(String strthrows IOException {
        writeUTF(strthis);
    }

    
Writes a string to the specified DataOutput using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to out as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

Parameters:
str a string to be written.
out destination to write to
Returns:
The number of bytes written out.
Throws:
IOException if an I/O error occurs.
    static int writeUTF(String strDataOutput outthrows IOException {
        int strlen = str.length();
        int utflen = 0;
        int ccount = 0;
        /* use charAt instead of copying String to char array */
        for (int i = 0; i < strleni++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }
        if (utflen > 65535)
            throw new UTFDataFormatException(
                "encoded string too long: " + utflen + " bytes");
        byte[] bytearr = null;
        if (out instanceof DataOutputStream) {
            DataOutputStream dos = (DataOutputStream)out;
            if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
                dos.bytearr = new byte[(utflen*2) + 2];
            bytearr = dos.bytearr;
        } else {
            bytearr = new byte[utflen+2];
        }
        bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
        bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
        int i=0;
        for (i=0; i<strleni++) {
           c = str.charAt(i);
           if (!((c >= 0x0001) && (c <= 0x007F))) break;
           bytearr[count++] = (bytec;
        }
        for (;i < strleni++){
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                bytearr[count++] = (bytec;
            } else if (c > 0x07FF) {
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
            } else {
                bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
            }
        }
        out.write(bytearr, 0, utflen+2);
        return utflen + 2;
    }

    
Returns the current value of the counter written, the number of bytes written to this data output stream so far. If the counter overflows, it will be wrapped to Integer.MAX_VALUE.

Returns:
the value of the written field.
See also:
written
    public final int size() {
        return ;
    }
New to GrepCode? Check out our FAQ X