error when base64String arrived from android to nodejs via socketio - java

i work on an app who will send image from android to nodejs via socket io and i got this error when i send data who has size like 700,00 KB in node js and i don't know how to someone can help me to work it better ???
This is my java code
File myFile = new File (selectedImagePath);
int ficher = (int) myFile.length();
System.out.println(""+size(ficher));
int file_size = Integer.parseInt(String.valueOf(myFile.length()/1024));
System.out.println(""+file_size);
size(ficher);
FileInputStream imageInFile = null;
try {
imageInFile = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte imageData[] = new byte[(int) myFile.length()];
try {
imageInFile.read(imageData);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Convert to base64;
String imageDatastring = Base64.encodeToString(imageData, SELECT_PICTURE);
socket.emit("img", imageDatastring, "MAK", "MAK");
And my javascript code (node js) is here
socket.on('img', function(msg, usr, username){
console.log(' Message from : '+username+' \n Message is : '+msg+"\n to : "+usr)
console.log(usernames[usr])
console.log(usernames)
io.sockets.socket(usernames[usr]).emit('event', msg, username);
fs.writeFile(username+ "- to -"+usr+'.png', msg, "base64", function(err){if(err){console.log(err)}})
});
This is error i got
AAAL9wOAGFzc2V0cy9z\nZXR0aW5nc2NyZWVuLnBuZ1BLBQYAAAAAmQCZAMgnAACf/w4AAAA\n","243
810840505","apk","23:10"]}
debug - emitting heartbeat for client ig4QpjbTaEgr20jOs6IA
Assertion failed: end <= source_len, file src\smalloc.cc, line 280
C:\xampp\htdocs\socketio_android-master\socketio_android-master>
I use socket io version 0.9.16
Thanks to all!!

Related

Send Audio (WAV) to web and play using sockets in Java

I have been stuck on this for awhile, and I have scoured the internet, and can't find any solutions. Pretty much I am trying to send a wav, using https://github.com/mrniko/netty-socketio. I do this by converting the WAV to binary (after reading it in) and then pushing it to the front end using the socket
The issue lays that the data is sent, it is converted to a blob, but the blob won't play, the browser siting a Uncaught (in promise) DOMException: Failed to load because no supported source was found. error.
Any ideas? There are multiple possible points of failure, but I can't figure it out.
Server.JAVA
File file = new File("src/main/resources/test.wav");
AudioInputStream in;
try{
try{
in = AudioSystem.getAudioInputStream(file);
}catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
}catch (UnsupportedAudioFileException e) {
System.out.println("Bad Audio File error");
e.printStackTrace();
return;
}
//CONVERT TO BYTE ARRAY
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
try{
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
}catch (java.io.IOException e) {
System.out.println("Can't read into buffer");
e.printStackTrace();
return;
}
final byte[] audio = buffer.toByteArray();
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
#Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
Client.js
var socket = io.connect('http://localhost:9092');
socket.emit('updateCoordinates');
socket.on('sound', function(file) {
console.log(file)
console.log("recieved");
var arrayBuffer = new Uint8Array(file).buffer;
console.log(arrayBuffer);
var blob = new Blob([arrayBuffer], {type : 'audio/wav'});
console.log(blob);
const audioUrl = URL.createObjectURL(blob);
const audio = new Audio(audioUrl);
audio.play();
});
Alright. I figured it out. The AudioSystem strips the wav of important metadata, so the front end could not read it. Updated code for the server working is
Path path = Paths.get("src/main/resources/test.wav");
final byte[] audio;
try{
audio = Files.readAllBytes(path);
}
catch (IOException e) {
System.out.println("Audio io error");
e.printStackTrace();
return;
}
//SERVER
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
config.setMaxFramePayloadLength(1024 * 1024);
config.setMaxHttpContentLength(1024 * 1024);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("updateCoordinates", byte[].class, new DataListener<byte[]>() {
#Override
public void onData(SocketIOClient client, byte[] data, AckRequest ackRequest) {
//System.out.println("Just gonna send it");
client.sendEvent("sound", audio);
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}

Client-server Java programm using sockets stucks while runnig, but works while debbuging

I have created a simple utility, that implements server and client. Server waits for incomming connection, client connects and if there are any files in folder on server - they are sending to client.
I used DataInput/OutputStreams - writeUTF/readUTF for sending messages between client and server, writeLong/readLong - to send file size, write/read to send data.
The problem is that when I run both client and server - working stucks after sending one or two files. But if I trace the client and server in IDE step-by-step it works great, and it also works if I transfer files with buffer size in one byte - so it works really slowly, but it does not stuck.
I use Eclipse IDE, jdk 1.7 on client side and 1.8 on server side (tried 1.7 also), Ubuntu linux and Windows 2003 Server on two instances of VMWare players.
I also tried to add Thread.sleep(10000) and flush() anything might be flushed, even added socket.setTcpNoDelay(true); but no result.
It looks like server sends the next file name and waits for echo() from client, but client does not get it and stucks on readUTF(). But, if I do it in manual mode step-by-step it works fine.
Any ideas? Thank you!
Client source:
/**
* Method receives a file from server
* #param outWriter
* #param inBufferedReader
* #param inStream
*
*/
private void receiveFileFromServer(DataOutputStream out, DataInputStream in) {
try {
String fileName = in.readUTF(); ///<----- stucks here *************
if (awlConnectionLogger.isInfoEnabled()) {
awlConnectionLogger.info("Receiving file "+fileName);
}
out.writeUTF(fileName);
out.flush();
Long fileSize = in.readLong();
if (awlConnectionLogger.isInfoEnabled()) {
awlConnectionLogger.info("Filesize "+fileSize);
}
out.writeUTF(fileSize.toString());
out.flush();
File localFile = new File(fileName);
FileOutputStream fileOS = new FileOutputStream(localFile);
byte[] buffer = new byte [2048];
int bytesRead = 0;
try {
for (int i = 0; i<fileSize/buffer.length; i++) {
bytesRead = in.read(buffer, 0, buffer.length);
fileOS.write(buffer,0, bytesRead);
}
bytesRead = in.read(buffer, 0, (int)(fileSize%buffer.length));
fileOS.write(buffer,0, bytesRead);
System.out.println("----------------------- "+bytesRead);
} catch (IOException e) {
awlConnectionLogger.error("Error reading file "+fileName);
};
fileOS.close();
if (awlConnectionLogger.isInfoEnabled()) {
awlConnectionLogger.info("File received.");
}
} catch (IOException e) {
awlConnectionLogger.error("Error reading Stream or socket closed");
}
}
/**
* Method sends a #param command to server, waits for answer, then if answer is not null and equals #param answer it logs #param messageOk else #param messageError
* returns true if everything is ok, else returns false
* #param outWriter
* #param inBufferedReader
* #throws IOException
*/
private boolean sendCommandReceiveAnswer(String command, String answer, String messageOk,String messageError,
DataOutputStream out, DataInputStream in) throws IOException {
out.writeUTF(command); //Hello handshake
out.flush();
String data = in.readUTF();
if ((data!=null)&&(data.equals(answer))) {
if (awlConnectionLogger.isInfoEnabled()) {
awlConnectionLogger.info(messageOk);
}
return true;
} else {
awlConnectionLogger.error(messageError);
return false;
}
}
/**
* Try to establish connection with the awl-server according to detected RDP session
* #param serverIP - ip address of server
* #param user - username
*/
private void establishConnection(String serverIP, String user) {
if (awlConnectionLogger.isInfoEnabled()) {
awlConnectionLogger.info("Trying to establish awl-connection to "+serverIP+" as "+user);
}
Integer remoteAwlPort = Integer.parseInt(Config.awlPort);
try (Socket awlServerSocket = new Socket(serverIP,remoteAwlPort)) {//Creating autocloseable socket
this.awlServerSocket = awlServerSocket;
DataOutputStream out = new DataOutputStream(awlServerSocket.getOutputStream());
DataInputStream in = new DataInputStream(awlServerSocket.getInputStream());
if (!sendCommandReceiveAnswer("Hello-awl-client", "Hello-awl-server", "Server hello OK.",
"Unknown server type. Closing thread.", out, in)) {
return;
}; //Hello handshake - return if not successful
if (!sendCommandReceiveAnswer(user, "User-ok", "Username sent.", "Error sending username. Closing thread.", out, in)) {
return;
} //Sending username
String hostName = InetAddress.getLocalHost().getHostName(); //Getting client local hostname
if (!sendCommandReceiveAnswer(hostName, "Hostname-ok", "Local hostname "+hostName+" sent. Ready to receive files.",
"Error sending hostname. Closing thread.", out, in)) {
return;
} //Sending hostname
awlServerSocket.setTcpNoDelay(true);
while (isActive) {
receiveFileFromServer(out, in); //receiving files
out.writeUTF("Ready");
out.flush();
}
} catch (UnknownHostException e) {
awlConnectionLogger.error("Error in server IP adress");
} catch (SocketException e) {
awlConnectionLogger.error("Problem accessing or creating Socket.");
} catch (IOException e) {
awlConnectionLogger.error("General IO Error or Socket closed");
};
}
Server source:
void Echo (DataInputStream in) {
String echo = null;
try {
echo = in.readUTF();
System.out.println("Echo:"+echo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendToClient (File file, DataOutputStream out, DataInputStream in) {
System.out.println("Sending file name "+file.getName());
try {
out.writeUTF(file.getName());
out.flush();
Echo (in); //<--------- stucks here *******************
out.writeLong(file.length());
out.flush();
Echo (in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sending file");
byte [] buffer = new byte [2048];
FileInputStream fileIS = null;
try {
fileIS = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Error opening file "+file);
e.printStackTrace();
}
try {
Integer bytesRead;
int i = 0;
while((bytesRead=fileIS.read(buffer))>0) {
out.write(buffer, 0, bytesRead);
}
out.flush();
} catch (IOException e) {
System.out.println("Error reading file "+file);
e.printStackTrace();
};
System.out.println("File sent.");
try {
fileIS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run () {
DataOutputStream out = null;
DataInputStream in = null;
String data = null;
String user = null;
String clientHostName = null;
try {
socket.setTcpNoDelay(true);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("New incoming connection thread starded.");
try {
data = in.readUTF();
if (data.equals("Hello-awl-client")) {
System.out.println("Client hello OK.");
out.writeUTF("Hello-awl-server");
out.flush();
user = in.readUTF();
System.out.println("User: "+user);
out.writeUTF("User-ok");
out.flush();
clientHostName = in.readUTF();
System.out.println("Client hostname: "+clientHostName);
System.out.println("Server hostname: "+InetAddress.getLocalHost().getHostName());
out.writeUTF("Hostname-ok");
out.flush();
System.out.println("Ready to send files.");
while (isActive) {
File file = new File(ServerConfig.pdfFolder+"//"+user);
if (!file.isDirectory()) {
System.out.println("Error in path to user directory. Exiting thread");
return;
}
File [] files = file.listFiles();
for (File fileItem:files) {
while (!fileItem.renameTo(fileItem)) {
Thread.sleep(1000);
}; //waits while file is not busy to send it
if (socket.isOutputShutdown()) {
out = new DataOutputStream(socket.getOutputStream());
}
sendToClient (fileItem, out, in);
if (fileItem.delete()) {
System.out.println("File deleted.");
} else {
System.out.println("File not deleted.");
}
data = in.readUTF();
System.out.println(data);
}
}
} else {
System.out.println("Unknown connection type. Closing thread.");
return;
}
} catch (IOException | InterruptedException e) {
System.out.println("Connection error or can't sleep thread. Closing thread.");
return;
}
}

video streaming from Android without saving on sdcard

I have code:
private MediaRecorder recorder;
String hostname = "192.168.1.125";
int port = 1935;
Socket socket;
ParcelFileDescriptor pfd;
public void start()
{
try {
socket = new Socket(InetAddress.getByName(hostname), port);
pfd = ParcelFileDescriptor.fromSocket(socket);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.setOutputFile(pfd.getFileDescriptor());
// String filename = String.format("/sdcard/%d.mp4", System.currentTimeMillis());
//
// recorder.setOutputFile(filename);
try
{
recorder.prepare();
recorder.start();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
And Server side:
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args)
{
try
{
System.out.println("create sock");
ServerSocket svsock = new ServerSocket(1935);
System.out.println("accept");
Socket sock = svsock.accept();
System.out.println("buffer read");
FileOutputStream outFile = null;
String filename = String.format("%d.mp4", System.currentTimeMillis());
try {
outFile = new FileOutputStream(filename);
System.out.println(filename);
} catch (IOException e1) {
e1.printStackTrace();
}
InputStream is = new DataInputStream(sock.getInputStream());
byte[] byteBuffer = new byte[1024];
int allsize = 0;
while(sock.isConnected()) {
int size = is.read(byteBuffer);
if (size == -1){
break;
} else {
outFile.write(byteBuffer, 0, size);
}
allsize += size;
}
System.out.println("close size=" + allsize);
outFile.close();
sock.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("endmain");
}
}
I test it on Android 2.2.2 (HTC quiet brilliant) and all works fine. When I press "start" button Server create file and record data from stream to file. After this file is normally play in VLC player and etc.
But when I test it on Android 4.0.4 (Galaxy S2) Server create file and record data from stream to file but not play in VLC (and other players too) and give me error
mp4 error: MP4 plugin discarded (no moov,foov,moof box)
avcodec error: Could not open �codec demux error: Specified event object handle is invalid
ps error: cannot peek
main error: no suitable demux module for `file/:///C:/1345461283455.mp4'
I also try to upload this file to youtube, but after upload youtube give me error like file format is unsupported.
But Android 4.0.4 (Galaxy S2) succesfully create and then play file when I save it on phone memory (not stream to server)
I think problem maybe on server side, or something changed on android 4.0.4.
Please, help me.
Thanks in advance.
try this
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(pfd.getFileDescriptor());
I think it should solve the problem. Give it a try and see if it helps ...

Android: Dowloaded files got deleted when the app restarts

I download audio files from server using
try {
// URL url = new URL("http://commonsware.com/misc/test2.3gp");
URL url = new URL("http://192.168.0.2/supplications/"+fileName);
//URL url = new URL("http://www.msoftech.com/supplications/android/"+fileName);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
Log.v("log_tag", "Check: " +cd2);
Here PATH = "/data/data/packagename/sounds/filename
It works fine, audio file downloaded and played successfully, but my problem is when I click the home button and then restart the app means the folder with the downloaded audio was not found, ie, when exit the app means all the downloaded audios were deleted automatically. It throws the exception file not found.
For playing the downloaded file I used the code as below,
public void audioPlayer(String path, String fileName) throw FileNotFoundException
{
//set up MediaPlayer
FileInputStream fileInputStream = new FileInputStream(PATH+"/"+fileName);
//String command = "chmod 666 " + recordFile.toString();
try {
mp.setDataSource(fileInputStream.getFD());
// mp.setDataSource(path+"/"+filename.mp3);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
whats the problem with it what I have to do for saving the audio file permenantly.
There is nothing problem in your code it is a file permission issue.
When you download file into internal file system under application package a security is assigned to it like "-rw------" this means your file is accessible for the same application only.As android is on Linux based so every file have some permission.
Your file would be there but not accessible to other application like media player etc, so these application throws error like file not found.(you can check though DDMS tool).
Just change the file path to external drive.
Accept the answer if it is helpful.

how to read html content from assets folder in android [duplicate]

This question already has answers here:
Loading html file to webview on android from assets folder using Android Studio
(3 answers)
Closed 5 years ago.
try {
File f = new File( "file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)") ;
FileInputStream fis= new FileInputStream(f);
System.out.println("_______YOUR HTML CONTENT CODE IS BELLOW WILL BE PRINTED IN 2 SECOND _______");
Thread.sleep(2000);
int ch;
while((ch=fis.read())!=-1)
{
fileContent=fileContent+(char)ch; // here i stored the content of .Html file in fileContent variable
}
System.out.print(fileContent);
//}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is my code. I want to read html content from asstes folder my file is available in asstes folder But it gives exception FileNotFoundException. So plz any one tell me how to read html content from asstes folder in android?
File f = new File( "file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)") ;
when i debug f gives= file:/android_asset/[2011]011TAXMANN.COM00167(PATNA)
plz tell me how to get corrct directory and where i m doing wrong it shud me coming file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)
This is the way to load HTML file from assets in WebView
webview.loadUrl("file:///android_asset/Untitled-1.html");
Untitled-1.html---File name that should be save first as .html extension
Edited
try this link
http://developer.android.com/reference/android/content/res/AssetManager.html
there is method from this doc
public final String[] list (String path)
You cat get InputStream by this code:
getResources().getAssets().open("you_file_name_goes_here");
you don't want to use
webview.loadUrl('file:///android_asset/htmlFile.html');
right?
try this i found it in a blog:
static String getHTMLDataBuffer(String url,Context context) {
InputStream htmlStream;
try {
if (Utils.isReferExternalMemory() && url.contains("sdcard")) {
String tempPath = url.substring(7, url.length());//remove file:// from the url
File file = new File(tempPath);
htmlStream = new FileInputStream(file);
}else{
String tempPath = url.replace("file:///android_asset/", "");
htmlStream = context.getAssets().open(tempPath);
}
Reader is = null;
try {
is = new BufferedReader(new InputStreamReader(htmlStream, "UTF8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// read string from reader
final char[] buffer = new char[1024];
StringBuilder out = new StringBuilder();
int read;
do {
read = is.read(buffer, 0, buffer.length);
if (read>0) {
out.append(buffer, 0, read);
}
} while (read>=0);
return out.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
usage:
String data = getHTMLDataBuffer("file:///android_asset/yourHtmlFile",this);
webview.loadDataWithBaseURL("http://example.com", data, "text/html", "utf-8", null);
Sorry for my bad english :)

Categories

Resources