Reading the same file but getting different bytes - scala

In a foldleft, I am trying to digest different images. I got frustrated and decided to read the same file and I received different byte arrays for reading the same file! Does anyone know why this is happening? Here is the code where it is happening, "swap.png" is never changed during the run
val capturedImage = outputScreen.capture
val swap = new File("swap1.png")
ImageIO.write(capturedImage, "png", swap)
val bis = new BufferedInputStream(new FileInputStream("swap.png"))
val byte = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
println(byte.toString)
Here is some output
[B#31302ed5
[B#3a56d071
[B#6039d718
[B#3490e5eb
[B#2f29608e
[B#4c09c5c3

What you're getting is toString representation of array of bytes (because of [B) which defaults to Object.toString(). What does Object.toString do? Typically, it just prints internal info about where object is in the memory (so called identity hash)*. So it is perfectly fine to have different objects at different RAM memory locations at different runs thus different output. Try instead of doing this issue .mkString(",") in last line:
println(byte.mkString(","))
* but there might be different implementations

Related

Xuggler write and read video via H.264 to/from Sockets

I want to be able to send BufferedImages generated from my java program over the local network in real time, some my second application can show them.
I have been looking through a lot of websites over the last 2 days but I wasn't able to find anything. Only thing I found was this:
Can I use Xuggler to encode video/audio to a byte array?
I tried implementing the URLHandler but problem is, MediaWriter still wants an URL and as soon as I add a VideoStream, it opens the container a second time with the url and then in crashes.
I hope you can help me and thanks in advance.
Code I have right now:
val clientSocket = serverSocket.accept()
connectedClients.add(clientSocket)
val container = IContainer.make()
val writer = ToolFactory.makeWriter("localhost", container)
container.open(VTURLProtocolHandler(clientSocket.getOutputStream()), IContainer.Type.WRITE, IContainerFormat.make())
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, width, height)

Invalid field in source data: 0 TCP_Message

I'm using ProtoBuf-Net for serialize and deserialize TCP_Messages.
I've tried all the suggestions I've found here, so I really don't know where the mistake is.
The serialize is made server side, and the deserialize is made on an application client-side.
Serialize code:
public void MssGetCardPersonalInfo(out RCPersonalInfoRecord ssPersonalInfoObject, out bool ssResult) {
ssPersonalInfoObject = new RCPersonalInfoRecord(null);
TCP_Message msg = new TCP_Message(MessageTypes.GetCardPersonalInfo);
MemoryStream ms = new MemoryStream();
ProtoBuf.Serializer.Serialize(ms, msg);
_tcp_Client.Send(ms.ToArray());
_waitToReadCard.Start();
_stopWaitHandle.WaitOne();
And the deserialize:
private void tpcServer_OnDataReceived(Object sender, byte[] data, TCPServer.StateObject clientState)
{
TCP_Message message = new TCP_Message();
MemoryStream ms = new MemoryStream(data);
try
{
//ms.ToArray();
//ms.GetBuffer();
//ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
message = Serializer.Deserialize<TCP_Message>(ms);
} catch (Exception ex)
{
EventLog.WriteEntry(_logSource, "Error deserializing: " + ex.Message, EventLogEntryType.Error, 103);
}
As you can see, I've tried a bunch of different approache, now comented.
I have also tried to deserialize using the DeserializeWithLengthPrefix but it didn't work either.
I'm a bit noob on this, so if you could help me I would really appreciate it.
Thank's
The first thing to look at here is: is the data you receive the data you send. Until you can answer "yes" to that, all other questions are moot. It is very easy to confuse network code and end up reading partial frames, etc. As a crude debugger test:
Debug.WriteLine(Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length));
should work. If the two base-64 strings are not identical, then you aren't working with the same data. This can be because of a range of reasons, including packet splitting and combining. You need to keep in mind that in a stream, what you send is not what you get - at least, not down to the fragment level. You might "send" data in the way of:
one bundle of 20 bytes
one bundle of 10 bytes
but at the receiving end, it would be entirely legitimate to read:
1 byte
22 bytes
7 bytes
All that TCP guarantees is the order and accuracy of the bytes. It says nothing about their breakdown in terms of chunks. When writing network code, there are basically 2 approaches:
have one thread that synchronously reads from a stream and local buffer (doesn't scale well)
async code (very scalable), but accept that you're going to have to do a lot of "do I have a complete frame? if not, append to an input buffer; if so, process any available frame data (could be multiple), then shuffle any incomplete data to the start of the buffer"

getblast error unavilable matlab

I'm writing matlab code that's supposed to iterate over a list sequences and blast each at a time. Here is the relevant part of the code:
%blast the seq
[res, ROTE] = blastncbi(seq, 'blastn');
res1 = getblast(res, 'WaitTime',ROTE);
resName = res1.Hits(1).Name
for some seq's it worked, and then for the last it gave me this error message:
Error using getblast (line 176)
BLAST V7EBUE0901R is unavailable - try later.
please note that I've defined ROTE as the 'WaitTime' value, as suggested in the documentation of this function.
The script must iterate over lots and lots of genes, so I can't let it crash every 5 minutes!
The RTOE returned by blastncbi is an estimated time of how long it takes. Perhaps the estimate is sometimes simply incorrect.
Two simple ways to deal with this could be waiting longer, or trying it twice:
res1 = getblast(res, 'WaitTime',ROTE*10);
or
try
res1 = getblast(res, 'WaitTime',ROTE);
catch
res1 = getblast(res, 'WaitTime',ROTE);
end
Of course this assumes that you are sure that the info that you request is actually available.

sending image as byte array from matlab to java through sockets

I am trying to send images from a matlab server to a java client through a sockets. First I converted the image to a byte array and I used the readFully method on the client side which requires a length to be specified.
The problem is that the size of the byte array changes from one image to the other and when I send the size through the write () method it does not read correctly on the client side.
Here is a snippet of my code
Matlab server
%% convert image
Javaimage=im2java2d (image);
Arraystream=ByteArrayOutputStream;
ImageIO.write (javaimage,'jpg', arraystream)
Arraystream.flush ();
Bytearray=arraystream.toByteArray;
%% send size through output stream
Outstream=write (size (byteArray, 1))
%% send array
outStream=write (byteArray);
On the client side I have tried many methods such as read, readInt, readDouble, but I think the trouble is the write () method I tried to use writeInt () but matlab did not recognize it even though I included the libraries needed:
Java.io
Java.net
Java.io.dataoutputstream
Java.io.datainputstream
I should also include that the size of the array is usually over 10000
I would appreciate any help
Thank you in advance
I have finally found the answer which is so simple that it was the last thing to check.
When I created the socket input and output streams I did the following
InStream =socket.getInputStream;
OutStream=socket.getOutputStream;
This allows you to use the methods from the inputStream class such as read () and write() which are methods that only read bytes.To read any other types of data we must use the dataInputStream and dataOutput stream classes which are a type of filter class that allow reading primitive types such as int , double , etc using methods such as readInt () and writeInt
To do this you need to change the above as follows
Instream=dataInputStream (socket.getInputStream);
OutStream=dataOutputStream (socket.getOutputStream);
And its that simple.

Best way for limit rate downloads in play framework scala

Problem: limit binary files download rate.
def test = {
Logger.info("Call test action")
val file = new File("/home/vidok/1.jpg")
val fileIn = new FileInputStream(file)
response.setHeader("Content-type", "application/force-download")
response.setHeader("Content-Disposition", "attachment; filename=\"1.jpg\"")
response.setHeader("Content-Length", file.lenght + "")
val bufferSize = 1024 * 1024
val bb = new Array[Byte](bufferSize)
val bis = new java.io.BufferedInputStream(is)
var bytesRead = bis.read(bb, 0, bufferSize)
while (bytesRead > 0) {
bytesRead = bis.read(bb, 0, bufferSize)
//sleep(1000)?
response.writeChunk(bytesRead)
}
}
But its working only for the text files. How to work with binary files?
You've got the basic idea right: each time you've read a certain number of bytes (which are stored in your buffer) you need to:
evaluate how fast you've been reading (= X B/ms)
calculate the difference between X and how fast you should have been reading (= Y ms)
use sleep(Y) on the downloading thread if needed to slow the download rate down
There's already a great question about this right here that should have everything you need. I think especially the ThrottledInputStream solution (which is not the accepted answer) is rather elegant.
A couple of points to keep in mind:
Downloading using 1 thread for everything is the simplest way, however it's also the least efficient way if you want to keep serving requests.
Usually, you'll want to at least offload the actual downloading of a file to its own separate thread.
To speed things up: consider downloading files in chunks (using HTTP Content-Range) and Java NIO. However, keep in mind that this will make thing a lot more complex.
I wouldn't implement something which any good webserver should be able to for me. In enterprise systems this kind of thing is normally handled by a web entry server or firewall. But if you have to do this, then the answer by tmbrggmn looks good to me. NIO is a good tip.