About RandomAccessFile/Write byte

Do you know the result and why ??
public static void main(String[] args) throws IOException{
RandomAccessFile raf = new RandomAccessFile("test.dat","rw");
raf.writeInt(0x01);
raf.writeShort(0x23);
raf.writeByte(0x45);
raf.seek(3);
short a1 = raf.readShort();
short a2 = raf.readShort();
System.out.println("0x"+Integer.toHexString(a1)+", 0x"+Integer.toHexString(a2));

The bytes are written as 0 0 0 1 0 35 69in the "test.data" file. Because you set the file pointer at the index 3 through seek(3), the bytes to be read are
1 0 35 69and if byte array {1,0} is read as a Short object it becomes 0x100, and the remaining byte array {35, 69} as a Short object is 0x2345.

Similar Messages

  • Static final buffer passed into FilterOutputStream.write(byte[])

    Greetings,
    My team is using a custom subclass of FilterOutputStream to XOR some bytes with a key before writing them to an underlying OutputStream. We have never had problems with it until we started testing with Java 1.4.x -- where we noticed that some optimizations had been made to GZIPOutputStream to write the GZIP header out at once using a static final buffer to hold the header.
    The problem seems to be that this header, although marked as static final, can still be changed when it is passed to our custom FilterOutputStream's write(byte[] b) method. For speed, we are XORing the bytes in place in the byte[] and then writing it directly back out. So every time we write the header with the same key, the static final byte[] that holds the header in GZIPOutputStream changes from its XORed to cleartext equivalent, and back.
    My question is this: should this be considered a bug in the SDK, or a bug in our code? Maybe I'm looking in the wrong place, but I can't find any documentation that specifies what can and can't be done with a byte[] that is passed into the write(byte[]) method of an OutputStream.
    Any input is appreciated.
    Regards,
    Mike Pontillo

    Mike,
    Need some code here to clarify. Are you saying that
    the original final static (constant) changes?
    You've got to be careful when you talk about passing a
    byte[], which can't be done, of course. You are
    passing a reference to a byte[]. Using the reference,
    the bytes can be copied. There aren't any restrictions
    on what can be done to the copy.
    and also, if you have final array, then only that arrays'
    reference is final, but the content of array may change
    as well as when that array wasn't static.Yes, I understand this. Thank you for your replies. FilterOutputStream.write(byte[]) of course takes a reference to a byte[]. We are overloading this to be something like:
    public void write(byte[] b) {
       for(int i = 0 ; i < b.length ; i++) {
          b[i] = foo(b);
    out.write(b);
    i.e. we modify the array inside the method rather than making a copy and writing out the copy.
    This hasn't been a problem until Java 1.4.x -- now GZIPOutputStream passes a private static final byte[] (reference of course) into write(byte[]) in OutputStream. So when our overloaded write(byte[]) method gets called in FilterOutputStream and code similar to the above runs, the private static final reference to a byte[] is overwritten in the java.util.zip.GZIPOutputStream class. This was a change sometime between 1.3 and 1.4, I believe. From the J2SDK 1.4.1_02 source:
         * Writes GZIP member header.
        private final static byte[] header = {
            (byte) GZIP_MAGIC,                // Magic number (short)
            (byte)(GZIP_MAGIC >> 8),          // Magic number (short)
            Deflater.DEFLATED,                // Compression method (CM)
            0,                                // Flags (FLG)
            0,                                // Modification time MTIME (int)
            0,                                // Modification time MTIME (int)
            0,                                // Modification time MTIME (int)
            0,                                // Modification time MTIME (int)
            0,                                // Extra flags (XFLG)
            0                                 // Operating system (OS)
        private void writeHeader() throws IOException {
            out.write(header);
        }As you can see, they are passing a private static final reference to a byte array into somebody's untrusted method (ours). =)
    Due to this issue we have to change our code so that it makes a copy of these bytes rather than changing them in the method. Now I'm wondering if this should be considered a bug in Java or not -- does changing bytes like this violate the general contract of an Input/Output stream? It at least seems like a bad idea for encapsulation purposes since anyone can change these bytes in the GZIPOutputStream class. We had previously assumed that if they were writing these bytes to an output stream they didn't care about them any more, which is the case 99.9% of the time in our experience.

  • Explain about the cost, bytes & cardinality calculation in oracle9i

    Hi All,
    Any one can explain about the cost, bytes & cardinality calculation in oracle 9i.
    Actually I want to understand the behaviour of execution plan taken by oracle.
    Here is one example.
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=51 Card=1 Bytes=424)
    1 0 SORT (GROUP BY) (Cost=51 Card=1 Bytes=424)
    2 1 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_ADDRESS_M'
    3 2 NESTED LOOPS (Cost=43 Card=1 Bytes=424)
    4 3 NESTED LOOPS (Cost=43 Card=1 Bytes=350)
    5 4 NESTED LOOPS (Cost=43 Card=1 Bytes=296)
    6 5 NESTED LOOPS (Cost=40 Card=1 Bytes=242)
    7 6 NESTED LOOPS (Cost=15 Card=1 Bytes=193)
    8 7 NESTED LOOPS (Cost=13 Card=1 Bytes=164)
    9 8 NESTED LOOPS (Cost=10 Card=1 Bytes=138)
    10 9 NESTED LOOPS (Cost=9 Card=1 Bytes=125)
    11 10 NESTED LOOPS (Cost=7 Card=1 Bytes=57)
    12 11 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_BRANCH_M' (Cost=2 Card=1 Bytes=20)
    13 12 INDEX (UNIQUE SCAN) OF 'NBFC_BRANCH_M_PK' (UNIQUE) (Cost=1 Card=1)
    14 11 TABLE ACCESS (BY INDEX ROWID) OF 'CS_ALLOCATION_HDR' (Cost=5 Card=1 Bytes=37)
    15 14 INDEX (RANGE SCAN) OF 'CS_ALLOCATION_QUEING' (NON-UNIQUE) (Cost=2 Card=2)
    16 10 TABLE ACCESS (BY INDEX ROWID) OF 'LEA_AGREEMENT_DTL' (Cost=2 Card=1 Bytes=68)
    17 16 INDEX (UNIQUE SCAN) OF 'LEA_AGREEMENT_DTL_UQ' (UNIQUE) (Cost=1 Card=1)
    18 9 INDEX (UNIQUE SCAN) OF 'SYS_C0016578' (UNIQUE) (Cost=1 Card=1 Bytes=13)
    19 8 TABLE ACCESS (BY INDEX ROWID) OF 'LEA_ASSET_M' (Cost=3 Card=1 Bytes=26)
    20 19 INDEX (RANGE SCAN) OF 'LEA_ASSET_M_CDX1'(NON-UNIQUE) (Cost=2 Card=1)
    21 7 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_CUSTOMER_M' (Cost=2 Card=1 Bytes=29)
    22 21 INDEX (UNIQUE SCAN) OF 'NBFC_CUSTOMER_M_PK' (UNIQUE) (Cost=1 Card=1)
    23 6 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_CHEQUE_DTL' (Cost=25 Card=1 Bytes=49)
    24 23 INDEX (RANGE SCAN) OF 'NBFC_CHEQUE_DTL_IDX1'(NON-UNIQUE) (Cost=3 Card=23)
    25 5 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_ADDRESS_M' (Cost=3 Card=1 Bytes=54)
    26 25 INDEX (RANGE SCAN) OF 'UK_BPID_BPTYPE_ADDTYPE'(NON-UNIQUE) (Cost=2 Card=1)
    27 4 TABLE ACCESS (BY INDEX ROWID) OF 'NBFC_ADDRESS_M'
    28 27 INDEX (RANGE SCAN) OF 'NBFC_ADDRESS_M_CDX1' (NON-UNIQUE) (Cost=2 Card=2)
    29 3 INDEX (RANGE SCAN) OF 'NBFC_ADDRESS_M_CDX1' (NON-UNIQUE) (Cost=2 Card=2)
    Statistics
    1 recursive calls
    0 db block gets
    117 consistent gets
    108 physical reads
    0 redo size
    2181 bytes sent via SQL*Net to client
    1185 bytes received via SQL*Net from client
    1 SQL*Net roundtrips to/from client
    1 sorts (memory)
    0 sorts (disk)
    0 rows processed
    Actully I don't know how to post the thread in proper format so example is not formatted.
    Thanx.. Ratan

    Cardinality is the number of rows.
    Cost -- think that is relative.
    Bytes -- the amount of data?
    Tom Kyte has a really nice explanation of this in Effective Oracle by Design -- but I left it at work.

  • About to write C_TB1200_88

    Hello all. I'm about to write the C_tb1200_88 exam, and was wondering if there was any additional information on the coverage? The description below is much more vague than the C_tb1200_07 exam description. For example, "Standard Setup and Configuration" is covered on this test, but what module of tb1200 is that referring to? (I'm guessing the AIP)
    From what I hear, it's essentially the same exam with newer test material for version 8.8. 
    Competency     Importance     
    Way(s) to attain
    Topic     Primary     Alternative     Other
    Analysis     40%     
    Implementation Methodology +     TB1200          
    Mapping Customer Business Requirements +++     TB1000, TB1100, TB1200          
    Logistics Business Processes ++     TB1000          
    Financials Business Processes ++     TB1100          
    Application     60%     
    Logistics, Financials & Administration Tasks +++     TB1200     TB1100, TB1000     
    Standard Setup and Configuration +++     TB1200     TB1100, TB1000     
    User Defined Values and Fields ++     TB1200          
    Support Processes ++     TB1200
    Edited by: LazoRadakovic on Jun 27, 2011 7:27 PM

    I would say that the SAP Business One 8.8 Implementation and Support certification is different than previous exams. Over 20 implementation partners worldwide were surveyed before the 8.8 implementation and support exam was created.  They were asked what the most important skills and knowledge areas for a consultant.  Those categories were used to develop the new exam.
    Like previous exams, the certification is made up of multiple choice questions, however,  previous exams were more focused on tasks and windows inside the application.  As you can see from the breakdown of topics, more emphasis is now placed on higher level analytical skills, such as mapping customer business processes to standard SAP Business One processes. 
    I suggest you try the practice test to get a feel for the types of questions on the exam.  Another useful tool to help prepare for the exam is the Study Guide, which based on your previous post, it looks like you've been reviewing.  If you can answer all the questions in the study guide, I'm positive you can answer the questions in the exam.

  • FileWriter to write bytes

    Hi can I use the FileWriter class to write bytes arrays int a file or its exclusive for text?
    I used to use the common output streams (DATA and FILE) but for the appending issue I want to use the FileWriter. So, will it write correctly bytes instead of text?

    Hi can I use the FileWriter class to write bytes
    arrays int a file or its exclusive for text?For text.
    >
    I used to use the common output streams (DATA and
    FILE) but for the appending issue I want to use the
    FileWriter. So, will it write correctly bytes instead
    of text?Not really!

  • RandomAccessFile writes only around 2024  lines!

    Hi all,
    I have a slight problem.
    I have created a little program that helps me with some processing. I had a file with a list of 8000 products (some occuer more than once), and a seperate file with a list of prices for each of the products (in mixed up order). So wot I did was read the first product from the product file and then search for the price in the second file, get the value and write to a third file the product and the price with a tab in between (to create a tab delimited file).
    I am using 3 RandomAccessFiles, 2 i read from using raf.readLine() method and 1 write to using raf.writeChars(string)
    Everytime after writing line 2024 (or 2025) i get an IOException...
    (this happens after about a couple of mins as the alogirthm is quite slow but that doesnt concern me, better than sitting a whole month editing in Excel)
    Can anyone help??

    Thankyou for your help, i've worked out the problem.
    i was opening a randomaccessfile in a while loop without closing it!
    So was ending up with too many open files after 2025 or files open it gives up. n e way i fixed the problem.
    much appreciated.

  • Read/write bytes in unicode

    I have the next piece of code:
    String encryptionText = "\u4f45\ueb33\ua4ea\ufcd0\u9e52\u6faf\udcd8\u4aa6";
    File inputText, outputText;
    outputText = new File("outputCod.txt");
    FileInputStream fin;
    FileOutputStream fout;
    String key, text;
    byte[] bufText, bufKey;
    fout = new FileOutputStream(outputText);
    fout.write(encryptionText.getBytes("UTF-8"));
    fout.close();
    inputText = new File("outputCod.txt");
    fin = new FileInputStream(inputText);
    bufText = null;
    bufText = new byte [(int)inputText.length()];
    fin.read(bufText);
    fin.close();
    text = new String (bufText, "UTF-8");
    At the end i want to have in text the same thing as in encryptionText. But I dont. In encrytionText I have "\u4f45\ueb33\ua4ea\ufcd0\u9e52\u6faf\udcd8\u4aa6" and in text I have "\u4f45\ueb33\ua4ea\ufcd0\u9e52\u6faf?\udcd8\u4aa6". Any idea where that "?" comes from? encryptionText.getBytes("UTF-8") and bufText have the same content in the end.

    just_me_n wrote:
    why are they not valid? aren't all valid? \u0000 to \uffff? it;s not quite binary i need to put intro string...i need to put into string any kind of \uxxxx.No, they are not all valid (in particular, the surrogate pair range is treated differently). You should not be putting binary data into a string, only character data.

  • About method 'write(int b)' in class 'OutputStream'

    Hello, everyone,
    In OutputStream class, there is a method called "write(int b)". In the API Specification, the explanation of this method is: Writes the specified byte to this output stream.
    I am just wondering, if the type of b is 'int', then how can it say " to write the byte to the stream"?

    Because the lower 8 bits of an int are the same value as a byte.
    -1 ==> 11111111 11111111 11111111 11111111 ==> 11111111
    0 ==> 00000000 00000000 00000000 00000000 ==> 00000000
    1 ==> 00000000 00000000 00000000 00000001 ==> 00000001
    255 ==> 00000000 00000000 00000000 11111111 ==> 11111111
    So it's essentially the same thing. Just chops off the upper 24 bits. The other thing is it's not uncommon to maintain byte values in ints for unsigned-ness, and if the method took a byte, it would require an explicit cast do to possible loss of precision, whereas a byte can be implicitly cast to an int with no alteration to the actual value represented.

  • Unable to read/write bytes using 9870 with cRio 9151

    Hi everyone,
    I'm currently having some problems trying to use 2 ports of module NI 9870. I'm unable to read bytes even if I try to send some datas (I always get a Timeout) and I'm also unable to write some datas( I can't see nothing on Hyperterminal).
    Please find enclosed a snapshot of my Fpga code.
    Regard,
    Nicolas Kaminski, Toulouse France
    Attachments:
    ReadBytes.jpg ‏93 KB
    WriteBytes.jpg ‏30 KB

    Hello,
    Can you give us some other details:
    Are you sure that you provide power to the 9870 module?
    Do you have tested your system with an example? (NI-987x Serial Loopback.lvproj)
    DO you get something through that loopback?
    Regards,
    Mathieu P. | Certified LabVIEW Associate Developer
    National Instruments France
    #adMrkt{text-align: center;font-size:11px; font-weight: bold;} #adMrkt a {text-decoration: none;} #adMrkt a:hover{font-size: 9px;} #adMrkt a span{display: none;} #adMrkt a:hover span{display: block;}
    Journées techniques : des fondamentaux aux dernières technologies pour la mesure et le contrôle/comm...

  • Need info about DataInputStream - read (byte[] ) method

    Hi all,
    I would like to know when does
    datainputStream.read(byte[] buffer) method return?
    Here dataInputStream is an object of type DataInputStream.
    Does the method return only after buffer.length bytes are read from the input stream? or Will it return after some bytes are read , even if the number of bytes read is less than buffer.length?
    I need this info because while developing a J2ME application and running it in an emulator , the above method seems to return even if some bytes are read.But when the application is transferred to a Mobile device, the method does not seem to return if some bytes are read, and seems to wait until buffer.length is reached.

    one more?

  • Question about multiple writes on FilterRecord- outData

    Is that even possible?  I want to implement a filter Plug-in which allows the user to choose from various operations and apply them sequentially.
    But when i want to apply the second operation on the outData strange things happen.
    I also made some test plugin to try this out :
    //start code
    //taking first 100x100 rectangle
    Rect rect ;
    rect.top = 0;
    rect.left =0;
    rect.right = 100;
    rect.bottom = 100;
    gFilterRecord->inRect = gFilterRecord->outRect = rect;
    gFilterRecord->inLoPlane = gFilterRecord->outLoPlane = 0;
    gFilterRecord->inHiPlane = gFilterRecord->outHiPlane = 2;
    *gResult = gFilterRecord->advanceState();
    if (*gResult != noErr)
    return;
    //here i start messing with pixel first time
    //should set the rectangle to black
    uint8 *inrow = (uint8 *)gFilterRecord->inData;
    uint8 *outrow = (uint8 *)gFilterRecord->outData;
    for (int32 y = rect.top; y < rect.bottom; y++)
    for (int32 x = 0; x < gFilterRecord->outRowBytes; x++)
    outrow[x] = (uint8)0;
    inrow += gFilterRecord->inRowBytes;
    outrow += gFilterRecord->outRowBytes;
    //updating (?)
    *gResult = gFilterRecord->advanceState();
    //now, let's take some wider rectangle (200x200)
    Rect rect2 ;
    rect2.top = 0;
    rect2.left =0;
    rect2.right = 200;
    rect2.bottom = 200;
    gFilterRecord->inRect = gFilterRecord->outRect = rect2;
    gFilterRecord->inLoPlane = gFilterRecord->outLoPlane = 0;
    gFilterRecord->inHiPlane = gFilterRecord->outHiPlane = 2;
    *gResult = gFilterRecord->advanceState();
    //second mess  with pixel , i add 100 to all values, so i'm expecting to get some gray-ish rectangle where it was black before and lighter values in the remaining border.
    uint8 *inrow2 = (uint8 *)gFilterRecord->inData;
    uint8 *outrow2 = (uint8 *)gFilterRecord->outData;
    for (int32 y = rect2.top; y < rect2.bottom; y++)
    for (int32 x = 0; x < gFilterRecord->outRowBytes; x++)
    outrow[x] = inrow[x]+100;
    inrow2 += gFilterRecord->inRowBytes;
    outrow2 += gFilterRecord->outRowBytes;
    *gResult = gFilterRecord->advanceState();
    //endcode
    So, instead of getting the gray-ish rectangle i'm expecting, i only get the black one. Second write had no effect.
    Am i trying to do something wrong?
    Do i have to implement some pixel buffer on my own to apply multiple image processings? 
    Sorry for the long post and thanks to who will answer.

    OPS.
    I just noticed that in the 2° cycle i referenced the wrong pointer.
    I also noticed that inData keeps the initial values (even after advancestate()),
    so i have to use outdata for further processings.
    Sorry guys, i could avoid this thread 

  • Complaint about DVD writer...

    i think the weakness point of apple product especially macbook is on their DVD writer... i have been using Macbook since macbook unibody white colour, then change to Mac book pro 2.5 gb, then now i change again to macbook pro 2.9 ghz... and all of them my problem is always on the DVD writer... they stop functioning after like 1 year i owned each of my macbook... this happend again and again... i thought i have a bad luck with the dvd writer for my 1st macbook.. then after my 2nd macbook it happend again i still thinking it's a coincidence... but now not again !!!! my 3rd macbook dvd writer giving up again.... I am not an abusive type especially in using my DVD writer bec i didnt burn cd frequently... i didnt listen audio with cd/dvd....or i also didnt watch movie through dvd .... so i can consider myself not a frequent user of this dvd wirter but still it broken easily means that the quality of the DVD writer in Apple macbook is very very bad quality... can you do something to fix / change the manufacturer of ur DVD....i really really disappointed....
    thank you

    Thats because youre either
    A: filling them with dust
    B: mistreating them in loading a disk
    The superdrive is made by Panasonic actually, and is very reliable.
    new ones are only $39, by the way
    Apple doesnt put DVD burners in ANY current Macbook , so its not even a concern  (except for still carrying the older model macbook).
    means that the quality of the DVD writer in Apple macbook is very very bad quality.
    Actually having opened many of them, 9 times out of 10, its a dirty lens / dusty.
    Theyre made very well.

  • About Report Writer

    Hello Everyone!!
    We are a Fort&eacute; development team in Peru, and we were develop an
    application that can build a Report dynamicaly.
    In this application you can select the tables, fields, especify the
    query condition, (build the select statement by Drag and Drop) of any
    Database in the current environment. Also you can especify if you want
    send the report to the printer, screen of text file. You can especify
    too the Report Header and a preview.
    Regards
    Ronald Celis [email protected]
    Victor Navarro [email protected]
    Bruno Munoz [email protected]

    http://www.virtuosollc.com/PDF/Get_Reporter.pdf
    Here are some salient features:
    1) Report Painter has 90% of the functionality that Report Writer has and is easier to develop.
    2) The last time I did anything on it, from the SAP main menu, choose Information systems->Ad hoc reports->Report painter.
    3) Report Writer functions can be accessed from within the Report Painter.
    4) Report Painter/Report Writer has four main components: Sets, Libraries, Reports, Report Groups
    5) The main difference between the two RW and RP is the graphical user interface of the Report Painter.
    6) It is generally used in FI-SL reporting.
    For more information, please check this links.
    For Report Painter
    http://help.sap.com/saphelp_47x200/helpdata/en/66/bc7d2543c211d182b30000e829fbfe/content.htm
    For Report Writer
    http://help.sap.com/saphelp_47x200/helpdata/en/66/bc7dc143c211d182b30000e829fbfe/content.htm
    Hope this will help.

  • About NTFS write

    hi
    i installed FUSE 2.7 and NTFS-3g 1.3 for writing on an usbdisk. every time i mount usbdisk it shows following error:
    FATAL: Error inserting fuse (/lib/modules/2.6.9-42.ELsmp/kernel/fs/fuse/fuse.ko): Invalid module format..
    i also tried other versions of fuse, same errors.
    os rhel as4u4.
    dnot know what happened? plz advise.

    Try the following steps...
    $cd fuse-2.7
    $ ./configure --enable-kernel-module
    $ make
    Regards
    -Mahesh

  • BufferedWriter wont write

    Hello, i am having trouble using a BufferedWrited with Sockets. I can form a connection and receive data normally, but sending data doesnt do anything.
    Here is my code:
    private void buildSocket()
        OutputStream os = _socket.getOutputStream();
        _socketWriter = new BufferedWriter(new OutputStreamWriter(os));
    // then elsewhere...
    private void sendData(String textToSend)
        _socketWriter.write(textToSend, 0, textToSend.length());
    }I have omitted the try \ catch clauses for clarity.
    On a related matter, the following code wouldnt compile:
    _socketWriter.write(textToSend.getBytes());The compiler complained "method write(byte[]) note found in class java.io.BufferedWriter". But in the API, BufferedWriter inherits that method from Writer. I am using JDK 1.4.
    Thanks

    About the not writing: do you close the stream?
    About the write (byte[]): there is no such method - only a write (char[]) method.
    Kind regards,
    Levi

Maybe you are looking for