java buffer problem florida tech
Hey guys
I'm working on this java assignment it has to do with buffers take a look:
-----------------------------------------------
In this assignment, you will unblock buffered input records and
repack the byte data obtained into a new set of blocked files.
The records inside the blocks have varying sizes according the
following scheme.
The data types are character (c), string (s), integer (d) and float(f).
The size of the data of the given type will also be encoded, with
the exception of character data, which will always have size 1.
The data layout will encoded as a format string, similar but not
equivalent to C I/O format strings:
(xiyjzk....) where x, y, z and so on are the data type indicators
and i, j, k and so on are the size indicators.
The parenthesis symbols, "(" and ")" are part of the format
string.
Since character data is always length 1, the length indicator is
omitted. Floating point data is formatted as l.r where l is the
number of decimal places to the left of the decimal and r is the
number of places to the right of the decimal point.
BNF:
format-string ->
"("{decimal-format|string-format|float-format|char-format}+")"
decimal-format -> "d"digit
string-format -> "s"digit
float-format -> "f"digit"."digit"
char-format -> "c"
digit -> 1|2|3|4|5|6|7|8|9
For instance (s5d3) would mean a string of length 5 followed by
an integer taking up three bytes (as an ASCII value).
A more robust format string might be
(s5d3cf3.5s2cd3d3f1.2)
parsed as s5, d3, c, f3.5, s2, c, d3, d3, f1.2
Data:
The length of the data will be the sum of the digits.
Data immediately follows the format string in the buffer:
(s5d3cf3.5s2cd2d4f1.2)keith123q12312345xyz98765432 1
The data is then interpreted as follows:
s5: keith
d3: 123
c: q
f3.5: 123.12345
s2: xy
c: z
d2: 98
d4: 7654
f1.2: 3.21
The next record follows immediately in the buffer:
(s5d3cf3.5s2cd3d3f1.2)keith123q12312345xyz98765432 1(d1d1cs2)12abc(d5)13579...
Use compile time -D to define buffer size. See
~kbg/302/C++/define.cpp. If the buffer is exhausted before the data
is, inspect the next buffer-full until all inputs are exhausted.
Outputs:
Your program must distribute all the string data to one file, all the
character data to another, all the integer data to yet another, and
all the floating point data to a fourth file. The same formatting
methods must be used to encode the data (WHERE APPROPRIATE!).
Thus the sample above would be distributed:
string file: (s5s2s2...)keithxybc....
or (s5)keith(s2)xy(s2)bc...
integer file (d3d3d3d1d1d5...)1239876541213579...
or (d3)123(d3)987(d3)654(d1)1(d1)2(d5)13579...
float file: (f3.5f1.2...12312345321
or (f3.5)12312345(f1.2)321
character file: (ccc...)qza...
or (c)q(c)z(c)a...
THEN, WHEN EACH OUTPUT BUFFER IS FULL (AND ONLY THEN) IT IS WRITTEN TO THE
CORRESPONDING FILE.
File inputs on the command line must be used.
Use _unformatted_ I/O: read, write.
I/O CAN ONLY BE DONE A BUFFER-FULL AT A TIME....
END OF FILE IS DETERMINED BY COUNTING THE NUMBER OF INPUT ITEMS,
(not by eof methods)
YOU MAY USE AT MOST 10 BUFFERS.
Example: assume a buffer size of 16, 2 input buffers, 1 output buffer per type.
the input: (s5f5.4)ABCDE123451234(f3.4)1231234(d9c)987654321X
the first 16 bytes are read in
I1: (s5f5.4)ABCDE123 // to one input buffer
... the beginning and end of the format string are determined...
move the format and data to the string output BUFFER (not the file)
S1: (s5)ABCDE
move the format and data to the float output buffer (not the file)
F1: (f5.4)123
now the input buffer is exhausted. read in another:
I2: 451234(f3.4)1231
and finish the copy to the float buffer, which becomes
F1: (f5.4)123451234 // this now has 15 items in it.
now find the beginning and end of the next format string and
in buffer I2. (THE END OF THE FORMAT STRING NEED NOT BE IN THE
BUFFER!)
But when the move to the float buffer is attempted, there
isn't enough room!!! so, fill as much as will fit; then write out the
float buffer:
so, the float buffer, F1, becomes (f5.4)123451234( ie. gets filled up.
the write of the buffer does not change its contents.
then the rest of the current float format string is memcpy'ed (hint)
into the buffer:
(f5.4)123451234(
F1: f3.4))123451234( ... notice what happens
^
this is where the next data is placed.
then the data is copied in behind the format string:
F1: f3.4)12313451234(
^
this is where the next data is placed.
... but the input buffer is exhausted! again. --
read in some more over writing the first input buffer:
I1: 234(d9c)98765432
and finish the copy into the float buffer:
F1: f3.4)12312341234(
^
this is where the next data is placed.
now process format string (d9c) in buffer I1....
but before the processing is finished, the buffer is exhausted,
so...
At this point in the computation, only one output has been performed:
that of the full buffer of float data. There have been three reads to fill
the input buffers.
END OF EXAMPLE...
Assumptions / other issues:
1. The formating digit will be between 1-9, inclusive.
2. Validity checking: will format and data fit in two buffers? If not, exit
gracefully.
(f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f 9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9. 9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f 9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9.9f9. 9f9.9f9.9f9.9f9.9f9.9f9.9)
-------------------------------------------------
Seems complicated. And I can't understand how to do this program. Anyone familiar with this kind of problem.
Help will be appreciated. Thank you
Re: java buffer problem florida tech
Quote:
unblock buffered input records and repack the byte data obtained into a new set of blocked files.
Your professor must have given you some background and notes towards designing a program to solve this problem.
There must be rules given for figuring out what is in the buffered input records
and for creating the new blocked output records.
If there are many different types of input, start with just one and work it through to the output.
get that part to work before adding more code for the rest of the input types.
Then add the next one and continue until you have them all.
Re: java buffer problem florida tech
No that text is all he has given me. I have tried making bytebuffers by putting the contents of input file in it through read, but cannot convert it to characters, and I cannot read the file directly into a CharBuffer according to java. I can't find a buffer code that can store the file data exactly like it in a buffer. And then basically I just have to separate the buffer into 4 other buffers containing different types of data according to the method shown and then written into corresponding files only once the buffers are full. I don't know what buffer code to use. Please help
Re: java buffer problem florida tech
Quote:
I don't know what buffer code to use
Please explain what you mean by "buffer code".
Can you explain your problem at a higher level?
For example: you have a file to read and you need to separate that file's contents to several different sections and write them to a file.
Re: java buffer problem florida tech
A file, specified by the user, has to be read into an input buffer, lets say I1. The data in the file contains 4 different types of data. decimal, String, float, character. In the file it is of the format example: (s5d3cf3.5s2cd3d3f1.2)keith123q12312345xyz98765432 1(d1d1cs2)12abc(d5)13579...
..where stuff inside brackets gives the format of the data that follows. s means string, d means decimal, c means character, f means float.
digit after the letter is the length of the data. eg s9 means a string of length 9.
This data has to be separated into the four different categories and stored into 4 corresponding files of the data using only buffers, as described in the assignment, and we are allowed to copy them to a file only after a buffer has been filled up, or the data in the file is exhausted.
We are not allowed to use end of file methods to determine when the input file ends. This has to be done by counting input items.
We can use any buffer, like bytebuffer or CharBuffer. This will have 2 input buffers, to read data once one buffer is exhausted. And we are allowed to use at most 10 buffers. I hope that clarifies everything. I had no idea what buffers were before I got this assignment. I researched on them but don't know how to implement one in this case. I would appreciate your help. Please let me know if you have any more questions.
Re: java buffer problem florida tech
Quote:
We can use any buffer, like bytebuffer or CharBuffer.
Is this an exercise to use classes from the nio package?
You keep saying 'buffer' meaning specific classes. I use buffer in a more generic sense, like you can use a byte array for a buffer.
Re: java buffer problem florida tech
Quote:
Originally Posted by
Norm
Is this an exercise to use classes from the nio package?
You keep saying 'buffer' meaning specific classes. I use buffer in a more generic sense, like you can use a byte array for a buffer.
No it does not say in the assignment. I just assumed so because that is what I found when I googled 'buffers in java'. If you think byte arrays act the same as buffers, and will fit the requirements of the assignment, I supposed I could use that as well.
But will bytebuffers also behave in the same way as assignment buffers. All the professor told me was this assignment was the way CD-ROMs worked, through buffers.
Re: java buffer problem florida tech
Most I/O done on computers uses buffers. That is a given.
I have not used the nio package classes and can not advise on what they offer. I'm sure that they will have a lot of useful features.
Re: java buffer problem florida tech
Quote:
Originally Posted by
computerbuff
No it does not say in the assignment. I just assumed so because that is what I found when I googled 'buffers in java'. If you think byte arrays act the same as buffers, and will fit the requirements of the assignment, I supposed I could use that as well.
But will bytebuffers also behave in the same way as assignment buffers. All the professor told me was this assignment was the way CD-ROMs worked, through buffers.
sorry in the last post in the last sentence i meant "can bytearrays also be considered to be buffers, and will they fit the assignment guidelines.".
Any advice on that.
Re: java buffer problem florida tech
I don't have any idea what the assignment means by buffers. As I said, I have not used the nio package classes. This assignment sounds like it wants you to use the classes in that package.
Re: java buffer problem florida tech
thank you. i'll see what i can do to make it work
Re: java buffer problem florida tech
(s5d3cf3.5s2cd3d3f1.2)
keith123q12312345xyz987654321
Do you understand the relationship between those two lines of data. First line starts with s5 which means that the second line contains a String with a length of 5 ie keith. d3 means a decimal with a length of 3 ie 123, etc.
Your first task would be to break the first line down into the individual tokens s5, d3, c, f3.5 etc. Work on doing that task and nothing else. Once you have got it working correctly move onto the next stage which would be to read the second line according to the tokens. Once you have that working correctly move onto inserting the data into the appropriate buffers. When that is working move onto writing the buffers to files.
One step at a time!