Java Code Examples for java.io.DataOutputStream#writeChar()

The following examples show how to use java.io.DataOutputStream#writeChar() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Java   File: DataStreamDemo.java    License: Artistic License 2.0 6 votes vote down vote up
private static void write() throws IOException {
	// DataOutputStream(OutputStream out)
	DataOutputStream dos = new DataOutputStream(new FileOutputStream(
			"dos.txt"));

	// ���
	dos.writeByte(1);
	dos.writeShort(10);
	dos.writeInt(100);
	dos.writeLong(1000);
	dos.writeFloat(1.1f);
	dos.writeDouble(2.2);
	dos.writeChar('a');
	dos.writeBoolean(true);

	// �ͷ���Դ
	dos.close();
}
 
Example 2
Source Project: Azzet   File: JsonWriter.java    License: Open Software License 3.0 6 votes vote down vote up
public static JsonWriter forOutputStream( final OutputStream oos )
{
	final DataOutputStream dos = new DataOutputStream( oos );

	return new JsonWriter() {

		public void write( String s ) throws IOException
		{
			dos.writeChars( s );
		}

		public void write( char c ) throws IOException
		{
			dos.writeChar( c );
		}
	};
}
 
Example 3
Source Project: djl   File: Parameter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Writes the parameter NDArrays to the given output stream.
 *
 * @param dos the output stream to write to
 * @throws IOException if the write operation fails
 */
public void save(DataOutputStream dos) throws IOException {
    if (!isInitialized()) {
        dos.writeChar('N');
        return;
    }

    dos.writeChar('P');
    dos.writeByte(VERSION);
    dos.writeUTF(getName());
    dos.write(array.encode());
}
 
Example 4
Source Project: PlusDemo   File: Io.java    License: Apache License 2.0 5 votes vote down vote up
private static void okio2() {
    try {
        Buffer buffer = new Buffer();
        DataOutputStream dataOutputStream = new DataOutputStream(buffer.outputStream());
        dataOutputStream.writeUTF("abab");
        dataOutputStream.writeBoolean(true);
        dataOutputStream.writeChar('b');
        DataInputStream inputStream = new DataInputStream(buffer.inputStream());
        System.out.println(inputStream.readUTF());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source Project: fitnotifications   File: Trie2_16.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a Trie2_16 onto an OutputStream.
 * 
 * A Trie2 can be serialized multiple times.
 * The serialized data is compatible with ICU4C UTrie2 serialization.
 * Trie2 serialization is unrelated to Java object serialization.
 *  
 * @param os the stream to which the serialized Trie2 data will be written.
 * @return the number of bytes written.
 * @throw IOException on an error writing to the OutputStream.
 */
public int serialize(OutputStream os) throws IOException {
    DataOutputStream dos = new DataOutputStream(os);
    int  bytesWritten = 0;
    
    bytesWritten += serializeHeader(dos);        
    for (int i=0; i<dataLength; i++) {
        dos.writeChar(index[data16+i]);
    }
    bytesWritten += dataLength*2;        
    return bytesWritten;
}
 
Example 6
Source Project: mochadoom   File: pcx_t.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(DataOutputStream f) throws IOException {
	// char -> byte Bytes.

	f.writeByte(manufacturer);
	f.writeByte(version);
	f.writeByte(encoding);
	f.writeByte(bits_per_pixel);
	
	// unsigned short -> char
	f.writeChar(Swap.SHORT(xmin));
	f.writeChar(Swap.SHORT(ymin));
	f.writeChar(Swap.SHORT(xmax));
	f.writeChar(Swap.SHORT(ymax));
    
	f.writeChar(Swap.SHORT(hres));
	f.writeChar(Swap.SHORT(vres));
	f.write(palette);
    
    f.writeByte(reserved);
    f.writeByte(color_planes);
 // unsigned short -> char
    f.writeChar(Swap.SHORT(bytes_per_line));
    f.writeChar(Swap.SHORT(palette_type));
    
    f.write(filler);
    //unsigned char	data;		// unbounded
    f.write(data);
}
 
Example 7
/**
 * Returns an OK response to the client
 * @param resp the response object
 * @param def the method definition
 * @param response the result from the method
 */
public static void writeResponse(HttpServletResponse resp, WSDefinition def, Character response) throws IOException {
    resp.setStatus(HttpServletResponse.SC_OK);
    DataOutputStream dos = new DataOutputStream(resp.getOutputStream());
    if(response != null) {
        dos.writeBoolean(true);
        dos.writeChar(response);
    } else {
        dos.writeBoolean(false);
    }
    dos.close();
}
 
Example 8
Source Project: xmnlp   File: BaseNode.java    License: Apache License 2.0 5 votes vote down vote up
protected void walkToSave(DataOutputStream out) throws IOException {
    out.writeChar(c);
    out.writeInt(status.ordinal());
    int childSize = 0;
    if (child != null) childSize = child.length;
    out.writeInt(childSize);
    if (child == null) return;
    for (BaseNode node : child) {
        node.walkToSave(out);
    }
}
 
Example 9
Source Project: SEANLP   File: FeatureFunction.java    License: Apache License 2.0 5 votes vote down vote up
public void save(DataOutputStream out) throws Exception {
	out.writeInt(o.length);
	for (char c : o) {
		out.writeChar(c);
	}
	out.writeInt(w.length);
	for (double v : w) {
		out.writeDouble(v);
	}
}
 
Example 10
@Override
public byte [] save() throws IOException {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(16);
	DataOutputStream dos = new DataOutputStream(bos);
	dos.writeChar(galaxySeed[0]);
	dos.writeChar(galaxySeed[1]);
	dos.writeChar(galaxySeed[2]);
	dos.writeInt(supernovaSystemIndex);
	dos.writeInt(state);
	dos.close();
	bos.close();		
	return bos.toByteArray();
}
 
Example 11
@Override
public byte [] save() throws IOException {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(16);
	DataOutputStream dos = new DataOutputStream(bos);
	dos.writeChar(galaxySeed[0]);
	dos.writeChar(galaxySeed[1]);
	dos.writeChar(galaxySeed[2]);
	dos.writeInt(targetIndex);
	dos.writeInt(state);
	dos.close();
	bos.close();		
	return bos.toByteArray();
}
 
Example 12
@Override
public byte [] save() throws IOException {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(16);
	DataOutputStream dos = new DataOutputStream(bos);
	dos.writeChar(galaxySeed[0]);
	dos.writeChar(galaxySeed[1]);
	dos.writeChar(galaxySeed[2]);
	dos.writeInt(targetIndex);
	dos.writeInt(state);
	dos.close();
	bos.close();		
	return bos.toByteArray();
}
 
Example 13
@Override
public byte [] save() throws IOException {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(16);
	DataOutputStream dos = new DataOutputStream(bos);
	dos.writeChar(galaxySeed[0]);
	dos.writeChar(galaxySeed[1]);
	dos.writeChar(galaxySeed[2]);
	dos.writeInt(targetIndex);
	dos.writeInt(state);
	dos.close();
	bos.close();		
	return bos.toByteArray();
}
 
Example 14
Source Project: QNotified   File: Table.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static void writeRecord(DataOutputStream out, @Nullable String key, Object val) throws IOException {
    byte type;
    try {
        Class clz = val.getClass();
        if (Byte.class.equals(clz)) {
            type = TYPE_BYTE;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeByte((byte) val);
        } else if (Boolean.class.equals(clz)) {
            type = TYPE_BOOL;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeByte(((boolean) val) ? 1 : 0);
        } else if (Character.class.equals(clz)) {
            type = TYPE_WCHAR32;
            out.writeInt(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeChar((Integer) val);
        } else if (Integer.class.equals(clz)) {
            type = TYPE_INT;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeInt((Integer) val);
        } else if (Short.class.equals(clz)) {
            type = TYPE_SHORT;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeShort((Short) val);
        } else if (Long.class.equals(clz)) {
            type = TYPE_LONG;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeLong((Long) val);
        } else if (Float.class.equals(clz)) {
            type = TYPE_FLOAT;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeFloat((Float) val);
        } else if (Double.class.equals(clz)) {
            type = TYPE_DOUBLE;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            out.writeDouble((Double) val);
        } else if (String.class.equals(clz)) {
            type = TYPE_IUTF8;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            writeIStr(out, (String) val);
        } else if (byte[].class.equals(clz)) {
            type = TYPE_IRAW;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            writeIRaw(out, (byte[]) val);
        } else if (Table.class.equals(clz)) {
            type = TYPE_TABLE;
            out.write(type);
            if (key != null) {
                writeIStr(out, key);
            }
            writeTable(out, (Table) val);
        } else {
            throw new IOException("Unsupported type:" + clz.getName());
        }
    } catch (NullPointerException e) {
        type = TYPE_VOID;
        out.write(type);
        if (key != null) {
            writeIStr(out, key);
        }
    }
}
 
Example 15
Source Project: netbeans   File: Archive.java    License: Apache License 2.0 4 votes vote down vote up
private static void writeString(DataOutputStream dos, String str) throws UnsupportedEncodingException, IOException {
    byte[] data = str.getBytes("UTF8");
    dos.writeChar(data.length);
    dos.write(data);
}
 
Example 16
@Test
public void testBytesRead() throws Exception
{
    byte[] testData;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    try
    {
        // boolean
        out.writeBoolean(true);
        // byte
        out.writeByte(0x1);
        // char
        out.writeChar('a');
        // short
        out.writeShort(1);
        // int
        out.writeInt(1);
        // long
        out.writeLong(1L);
        // float
        out.writeFloat(1.0f);
        // double
        out.writeDouble(1.0d);

        // String
        out.writeUTF("abc");
        testData = baos.toByteArray();
    }
    finally
    {
        out.close();
    }

    DataInputStream in = new DataInputStream(new ByteArrayInputStream(testData));
    BytesReadTracker tracker = new BytesReadTracker(in);

    try
    {
        // boolean = 1byte
        boolean bool = tracker.readBoolean();
        assertTrue(bool);
        assertEquals(1, tracker.getBytesRead());
        // byte = 1byte
        byte b = tracker.readByte();
        assertEquals(b, 0x1);
        assertEquals(2, tracker.getBytesRead());
        // char = 2byte
        char c = tracker.readChar();
        assertEquals('a', c);
        assertEquals(4, tracker.getBytesRead());
        // short = 2bytes
        short s = tracker.readShort();
        assertEquals(1, s);
        assertEquals((short) 6, tracker.getBytesRead());
        // int = 4bytes
        int i = tracker.readInt();
        assertEquals(1, i);
        assertEquals(10, tracker.getBytesRead());
        // long = 8bytes
        long l = tracker.readLong();
        assertEquals(1L, l);
        assertEquals(18, tracker.getBytesRead());
        // float = 4bytes
        float f = tracker.readFloat();
        assertEquals(1.0f, f, 0);
        assertEquals(22, tracker.getBytesRead());
        // double = 8bytes
        double d = tracker.readDouble();
        assertEquals(1.0d, d, 0);
        assertEquals(30, tracker.getBytesRead());
        // String("abc") = 2(string size) + 3 = 5 bytes
        String str = tracker.readUTF();
        assertEquals("abc", str);
        assertEquals(35, tracker.getBytesRead());

        assertEquals(testData.length, tracker.getBytesRead());
    }
    finally
    {
        in.close();
    }

    tracker.reset(0);
    assertEquals(0, tracker.getBytesRead());
}
 
Example 17
Source Project: Bytecoder   File: CodePointTrie.java    License: Apache License 2.0 4 votes vote down vote up
@Override int write(DataOutputStream dos) throws IOException {
    for (char v : array) { dos.writeChar(v); }
    return array.length * 2;
}
 
Example 18
public void testDataInputStream() throws Exception {
    String str = "AbCdEfGhIjKlM\nOpQ\rStUvWxYz";
    ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream da = new ByteArrayInputStream(str.getBytes());

    DataInputStream a = new DataInputStream(aa);
    try {
        Assert.assertEquals(str, read(a));
    } finally {
        a.close();
    }

    DataInputStream b = new DataInputStream(ba);
    try {
        Assert.assertEquals("AbCdEfGhIj", read(b, 10));
    } finally {
        b.close();
    }

    DataInputStream c = new DataInputStream(ca);
    try {
        Assert.assertEquals("bdfhjl\np\rtvxz", skipRead(c));
    } finally {
        c.close();
    }

    DataInputStream d = new DataInputStream(da);
    try {
        assertEquals("AbCdEfGhIjKlM", d.readLine());
        assertEquals("OpQ", d.readLine());
        assertEquals("StUvWxYz", d.readLine());
    } finally {
        d.close();
    }

    ByteArrayOutputStream e = new ByteArrayOutputStream();
    DataOutputStream f = new DataOutputStream(e);
    try {
        f.writeBoolean(true);
        f.writeByte('a');
        f.writeBytes("BCD");
        f.writeChar('e');
        f.writeChars("FGH");
        f.writeUTF("ijklm");
        f.writeDouble(1);
        f.writeFloat(2);
        f.writeInt(3);
        f.writeLong(4);
        f.writeShort(5);
    } finally {
        f.close();
    }

    ByteArrayInputStream ga = new ByteArrayInputStream(e.toByteArray());
    DataInputStream g = new DataInputStream(ga);

    try {
        assertTrue(g.readBoolean());
        assertEquals('a', g.readByte());
        assertEquals(2, g.skipBytes(2));
        assertEquals('D', g.readByte());
        assertEquals('e', g.readChar());
        assertEquals('F', g.readChar());
        assertEquals('G', g.readChar());
        assertEquals('H', g.readChar());
        assertEquals("ijklm", g.readUTF());
        assertEquals(1, g.readDouble(), 0);
        assertEquals(2f, g.readFloat(), 0f);
        assertEquals(3, g.readInt());
        assertEquals(4, g.readLong());
        assertEquals(5, g.readShort());
    } finally {
        g.close();
    }
}
 
Example 19
@Override int write(DataOutputStream dos) throws IOException {
    for (char v : array) { dos.writeChar(v); }
    return array.length * 2;
}
 
Example 20
@Override
public void saveScreenState(DataOutputStream dos) throws IOException {
	if (mediaPlayer != null) {
		mediaPlayer.reset();
	}		
	if (adder != null) {
		adder.setSaving(true);
	}
	dos.writeBoolean(flight != null);
	dos.writeBoolean(hyperspace != null);
	if (flight != null) {
		flight.saveScreenState(dos);
	}
	if (hyperspace != null) {
		hyperspace.saveScreenState(dos);
	}
	dos.writeInt(currentLineIndex); // 1 is subtracted in the read object code, because it has to be passed to the constructor.		
	dos.writeChar(savedGalaxySeed[0]);
	dos.writeChar(savedGalaxySeed[1]);
	dos.writeChar(savedGalaxySeed[2]);
	dos.writeInt(savedPresentSystem == null ? -1 : savedPresentSystem.getIndex());
	dos.writeInt(savedHyperspaceSystem == null ? -1 : savedHyperspaceSystem.getIndex());
	dos.writeInt(savedInstalledEquipment.size());
	for (Equipment e: savedInstalledEquipment) {
		dos.writeByte(EquipmentStore.ordinal(e));
	}
	dos.writeInt(savedFuel);
	for (int i = 0; i < 4; i++) {
		dos.writeInt(savedLasers[i] == null ? -1 : EquipmentStore.ordinal(savedLasers[i]));
	}
	for (int i = 0; i < Settings.buttonPosition.length; i++) {
		dos.writeInt(savedButtonConfiguration[i]);
	}	
	dos.writeBoolean(savedDisableTraders);
	dos.writeBoolean(savedDisableAttackers);
	dos.writeInt(savedMissiles);
	dos.writeLong(savedCredits);
	dos.writeInt(savedScore);
	dos.writeInt(savedLegalStatus.ordinal());
	dos.writeInt(savedLegalValue);
	dos.writeInt(savedInventory.length);
	for (InventoryItem w: savedInventory) {
		dos.writeLong(w.getWeight().getWeightInGrams());
		dos.writeLong(w.getPrice());
		dos.writeLong(w.getUnpunished().getWeightInGrams());
	}
	dos.writeLong(time);
	dos.writeInt(savedMarketFluct);
}