×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    ByteArrayInputStream and DataInputStream

    I always get confused and get headache whenever I read about stream.

    In the following program, I understand how to write data to the recordstore.
    The DataOutputStream class has methods that write specific data types to a buffer.
    Three of these methods are used in this example. These are writeUTF() method,
    writeBoolean() method, and writeInt() method. Each is passed the appropriate data.
    The buffered data is placed in the data stream (ByteArrayOutputStream ???) by calling the flush() method. The
    stream is converted to a byte array by calling the toByteArray() method, which returns
    a reference to the byte array of the stream. This reference is passed to the addRecord()
    method which saves the byte array as a new
    record in the record store. The ByteArrayOutputStream object's internal store is cleared
    by calling the reset() method.

    But I know nothing how to read data from a recordstore with ByteArrayInputStream and DataInputStream and how they work in detail.

    We have to write like that.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream outputDataStream =new DataOutputStream(outputStream);
    Why we need to pass the referende to the byte array output stream in the DataOutPutStream() constructor?

    Similarly,
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
    DataInputStream inputDataStream =new DataInputStream(inputStream);


    I try to find some explanation from google but I don't understand.
    Please point out some book or link if there is any thing u want to point out.
    But please kindly also explain me with your own words.

    Thank you very much.

    Code:
    try
    {
    byte[] outputRecord;
    String outputString = "First Record";
    int outputInteger = 15;
    boolean outputBoolean = true;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream outputDataStream =
    new DataOutputStream(outputStream);
    outputDataStream.writeUTF(outputString);
    outputDataStream.writeBoolean(outputBoolean);
    outputDataStream.writeInt(outputInteger);
    outputDataStream.flush();
    outputRecord = outputStream.toByteArray();
    recordstore.addRecord(outputRecord, 0, outputRecord.length);
    outputStream.reset();
    outputStream.close();
    outputDataStream.close();
    }
    catch ( Exception error)
    {
    alert = new Alert("Error Writing",
    error.toString(), null, AlertType.WARNING);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert);
    }
    try
    {
    String inputString = null;
    int inputInteger = 0;
    boolean inputBoolean = false;
    byte[] byteInputData = new byte[100];
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
    DataInputStream inputDataStream =
    new DataInputStream(inputStream);
    for (int x = 1; x <= recordstore.getNumRecords(); x++)
    {
    recordstore.getRecord(x, byteInputData, 0);
    inputString = inputDataStream.readUTF();
    inputBoolean = inputDataStream.readBoolean();
    inputInteger = inputDataStream.readInt();
    inputStream.reset();
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: ByteArrayInputStream and DataInputStream

    Quote Originally Posted by komomo View Post
    We have to write like that.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream outputDataStream =new DataOutputStream(outputStream);
    Why we need to pass the referende to the byte array output stream in the DataOutPutStream() constructor?

    ---> For storing in RMS via - addRecord(byte[] data, int offset, int numBytes) we need btyeArray byte[] which we can get only from ByteArrayOutputStream and not from DataOutputStream , which is needed for writing different data types.


    Similarly,
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
    DataInputStream inputDataStream =new DataInputStream(inputStream);
    Similarly while reading from RMS -- using --> recordstore.getRecord(int recordId, byte[] buffer, int offset) we get the record in byte[], hence we create ByteArrayInputStream from it as we can't create DataInputStream directly from byte[], and then read back the content of different data types in the same order as we wrote while writing to RMS.
    Thanks,
    Ekta

  3. #3
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: ByteArrayInputStream and DataInputStream

    We want to read data from a recordstore. Those data are stored in the recordstore as byte array.
    So we have to use DataInputStream with an underlying ByteArrayInputStream because DataInputStream only read primitive Java data types .
    That is why we have to use reference of ByteArrayInputStream as a parameter in the constructor of DataInputStream.

    In the ByteArrayInputStream, we have to use a byte array as a parameter. From that byte array, we will read bytes into the ByteArrayInputStream.
    To use DataInputStream with an underlying ByteArrayInputStream, we have to use ByteArrayInputStream as a prarmeter in the constructor of DataInputStream?
    We have to keep that in our mind by heart?

    Is that right? Can I explain like that if I have to explain about that.
    Now I can write a program that store data of mixed data type into a recordstore and read that data from a recordstore.
    The problem is I don’t know how to explain that reading and writing part if someone asks me to explain my program.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: ByteArrayInputStream and DataInputStream

    Your knowledge looks OK to me for now.
    Thanks,
    Ekta

Similar Threads

  1. Bug in DataInputStream or what ?
    By UGreek in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2009-03-28, 18:56
  2. Replies: 0
    Last Post: 2007-10-11, 13:36
  3. Playing WAV from ByteArrayInputStream vs. Resource
    By kevnews in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-07-17, 05:35
  4. [HttpConnection, DataInputStream] abort DataInputStream::read()
    By nokia_devil in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-06-06, 14:19
  5. DataInputStream
    By orenbaranes in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2004-01-20, 10:57

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •