file not found exception (No such directory) - java

I am trying to write to eeprom (at24) from user space in android.
I got the exception
com.ipil.ipilMdm.service.lib I/System.out: java.io.FileNotFoundException: /sys/devices/soc.0/78ba000.i2c/i2c-6/6-0050/eeprom (No such file or directory)
The path mentioned works perfect from echo and cat in command line. It is also tested in linux based application where it is opened as a read-write file.
I have added the permissions in manifest file. What else might be the issue?
Below is my code:
#Override
public void prod_eeprom_write(String data, int id) {
String someText = "Writesomething";
//String fileName = Environment.getRootDirectory().getAbsolutePath();
final File permFile = new File("/sys/devices/soc.0/78ba000.i2c/i2c-6/6-0050/eeprom");
try {
/* if (file == null) {
System.out.print("error open\n");
return;
}*/
//=================== Condition for action==========================
switch (id) {
case IMEI_NUMBER:
offset = IMEI_NUMBER_OFFSET;
length = IMEI_NUMBER_LENGTH;
break;
case HARDWARE_REVISION:
offset = HW_REV_OFFSET;
length = HW_REV_LENGTH;
break;
case WIFI_MAC_ADDRESS:
offset = WIFI_MAC_OFFSET;
length = WIFI_MAC_LENGTH;
break;
case BT_MAC_ADDRESS:
offset = BT_MAC_OFFSET;
length = BT_MAC_LENGTH;
break;
default : return;
}
/* now got offset and length, so update max buffer */
//strncpy(data,max_data.length + offset, length);
if (data.length() < length)
length = data.length();
if(data.length() > length){
System.out.print("LENGTH OF DATA IS TOO BIG : CAN NOT WRITE\nMAX LENGTH IS %d BYTES\n"+ length);
return;
}
//strncpy(data, max_data.length + offset, data.length());
max_data[INFO_MAX_LENGTH] = '\0';
System.out.println("DATA TO BE WRITTEN TO EEPROM: "+ data);
//if(file.canWrite()){
fileOutputStream = new FileOutputStream(permFile);
byte[] b = data.getBytes(); //new byte[max_data.length];
//for (int i = 0; i < b.length; i++)
// b[i] = (byte) max_data[i];
fileOutputStream.write(b, max_data.length + offset, data.length());
Log.i(TAG, " prod_eeprom_written-----------ok : hogya ");
Thread.sleep(10);
fileOutputStream.close();
System.out.println("success...");
}catch (Exception io){
io.printStackTrace();
}
}

Related

Extract binaries line by line in JAVA without saving to a string

I have the following method, but this one has a problem and it is that in a part of the file there are some special characters where the character with hexadecimal value 81(.) when the program generates the export of the file is 3F(?) and with that it damages the binary:
public static void exportbinary() {
List<String> files = listFiles(source,1);
System.out.println("Reviewing files to extract data");
String registro = null;
String contenido = "";
for (String s : files) {
//The name of the files to be processed is adjusted
String ogName=s;
System.out.println("Renaming files to extract binary data...");
File sFile = new File(source, s);
sFile.renameTo(new File(source,s+"ext"));
s = s+"ext";
//Start binary extraction process
File sourceFile = new File(source, s);
File destFile = new File(destination, ogName);
if (destFile.exists()) {
destFile.delete();
}
if (!dctmp.exists()) {
try {
dctmp.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Writer w = null;
try {
w = new BufferedWriter(new FileWriter(dctmp, true));
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Processing" + sourceFile + " en " + destFile);
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
String data;
while ((data = br.readLine()) != null) {
String line = data;
if (line.substring(0, 6).equals("924007")) {
// Valores binarios
String binarios = line.substring(585, 586 + 82);
line = line.substring(0, 585) + (new String(new char[83]).replace('\0', '0'))
+ line.substring((586 + 82), line.length());
// TID
String TID = line.substring(1005, 1020);
registro = TID + ";" + binarios + "\n";
contenido = contenido + registro;
}
BufferedWriter wrtr = new BufferedWriter(new FileWriter(destFile, true));
wrtr.append(line + "\n");
wrtr.close();
line = null;
}
System.out.println("Data is added to the temporary");
w.append(contenido);
w.close();
br.close();
System.out.println("Successfully complete process");
} catch (Exception e) {
e.printStackTrace();
} finally {
sourceFile.delete();
}
}
}
I did an exercise treating the file with FileInputStream() and it worked and kept the value, and it is the following code:
private void testBinary(File filename) {
try {
FileInputStream FileOrigin = new FileInputStream(filename);
// Arrays
byte[] biyeFile = new byte[(int) filename.length()];
int[] intArray = new int[biyeFile.length];
char[] CharArrayBit = new char[intArray.length];
FileOrigin.read(biyeFile);
for (int i = 0; i < biyeFile.length; i++) {
// Converting each char into its byte equivalent
intArray[i] = (int) Byte.toUnsignedInt(biyeFile[i]);
}
File temp = new File("C:/MOVIMIENTO/ebcdic/ascii/", filename.getName() + ".TMP");
temp.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(temp);
for (int j = 0; j < charArrayBit.length; ++j) {
charArrayBit[j] = (char) intArray[j];
try {
if ((intArray[j] != 10) && (intArray[j] != 13)) {
fileOutputStream.write(charArrayBit[j]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
FileOrigin.close();
fileOutputStream.close();
filename.delete();
temp.renameTo(filename);
} catch (IOException e) {
e.printStackTrace();
}
}
But I can't seem to implement the logic I made with the one that is already there and I'm getting stuck :( on how I can deal line by line in bytes. Try this Java - Read line using InputStream
The mess I have is that by using String I already lose the value String data; data = br.readLine() String line = data;
Update:
The idea is that the temporary file where I am printing the TID and binary is as follows:
file dc.tmp
200232133:..3....¢...€ . ...
300232133:.....Þù...€ ....€
400232133:ù....Þù...€ ....€ ....
etc
This is some VB.net code that I'm basing on pulling out the binary:
While POS < inputFile.Length
While reg < rowlen
dato1 = inputFile.ReadByte()
POS = POS + 1
reg = reg + 1
dato11 = BitConverter.GetBytes(dato1)
If (binary = 1 And (reg > 589 And reg <= 589 + 53 + 53)) Then
' here you should read two bytes and convert it to one
byte1 = dato1
' now read the second byte
byte2 = inputFile.ReadByte()
reg = reg + 1
POS = POS + 1
final = CInt("&H" & (Chr(byte1) + Chr(byte2)))
Else
dato2 = Encoding.Convert(source, DEST, dato11)
final = dato2(0)
End If
Print(1, Chr(final))
If (reg < 7) Then 'the first positions give us the type of message
MTI = MTI + Chr(dato1)
End If
If reg = 7 Then
binary = 0
rowlen = 1400
If (MTI = "924007") Then ' binary
binary = 1
rowlen = 1400 + 53
End If
MTI = ""
End If
End While
POS = POS + 2
reg = 0
End While
Thank you.

JAVA NIO package, getting unexpected network error occured

I have a code in which I read images on a network drive. i read thousands of images, but only sometimes i get following exception occasionally.
java.io.IOException: An unexpected network error occurred
at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at java.base/sun.nio.ch.FileDispatcherImpl.read(FileDispatcherImpl.java:54)
at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:223)
at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
at java.base/java.io.InputStream.read(InputStream.java:205)
below is the code for which i get it
`
public static int getEPSSectionOffset(File file) throws Exception {
int result = 0;
try (InputStream inputStream =
Files.newInputStream(Paths.get(file.getAbsolutePath()),StandardOpenOption.READ);) {
byte[] fourBytes = new byte[4];
int totalBytesRead = inputStream.read(fourBytes);
if (log.isDebugEnabled())
log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());
if (fourBytes[0] == (byte) 0xC5 && fourBytes[1] == (byte) 0xD0 && fourBytes[2] == (byte) 0xD3
&& fourBytes[3] == (byte) 0xC6) {
totalBytesRead = inputStream.read(fourBytes);
if (log.isDebugEnabled())
log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());
result = makeInt(fourBytes);
}
return (result);
} catch (Exception e) {
log.error("Get EPS Section Offset - " + e.getMessage(), e);
}
return 0;
}`
I get the exception at this line- int totalBytesRead = inputStream.read(fourBytes);
You probably have a issue with the underlying network connection. This is not a type of problem you can fix, there will always be intermittent problems with networks. This means that you will have to live with it, and mitigate the impact.
Maybe something like this:
public static int getEPSSectionOffsetWithRetry(File file) {
int retryCount = 3;
for(int i=0; i < retryCount i++) {
try {
int offset = getEPSSectionOffset()
return offset;
} catch (IOException ex) {
//Maybe wait i little
}
}
throw new IOException("Retry count exceeded");
}

Java - Write content from one file chunk by chunk (e.g. 8 Bytes) alternately into multiple files

So I've been trying to read the content of a text file and write the content chunk by chunk alternately into e.g. 2 new files.
I already tried multiple ways to do that but it won't work (OutputStream and FileOutputStream seems to be the most suitable).
Before i tried to part the file in e.g. 3 Parts and wrote the first part in one file, the second part in another and so on. Which worked perfectly fine with OutputStream and FileOutputStream.
But it won't work when i want to do it alternately.
To do it alternately i use the round robin algorithm, which on its own works fine.
I would be really thankful if you could show me some examples to do it!
public void splitFile(String filePath, int numberOfParts, long sizeOfParts[]) throws FileNotFoundException, IOException, SQLException {
long bytes = 8;
OutputStream partsPath[] = new OutputStream[numberOfParts];
long bytePositition[] = new long[numberOfParts];
long copy_size[] = new long[numberOfParts];
for (int i = 0; i < numberOfParts; i++) {
copy_size[i] = sizeOfParts[i];
partsPath[i] = new FileOutputStream(path); //Gets Path from my Database (works)
//System.out.println(cloudsTable.getCloudsPathsFromDatabase(i) + '\\' + name + (i + 1) + fileType);
}
InputStream file = new FileInputStream(filePath);
while (true) {
boolean done = true;
for (int i = 0; i < numberOfParts; i++) {
if (copy_size[i] > 0) {
done = false;
if (copy_size[i] > bytes) {
copy_size[i] -= bytes;
bytePositition[i] += bytes;
System.out.println("file " + i + " " + bytePositition[i]);
readWrite(file, bytePositition[i], partsPath[i]);
} else {
bytePositition[i] += copy_size[i];
System.out.println("rest file " + i + " " + bytePositition[i]);
readWrite(file, bytePositition[i], partsPath[i]);
copy_size[i] = 0;
}
}
}
if (done == true) {
break;
}
}
file.close();
for (int i = 0; i < partsPath.length; i++) {
partsPath[i].close();
}
}
private void readWrite(InputStream file, long bytes, OutputStream path) throws IOException {
byte[] buf = new byte[(int) bytes];
while (file.read(buf) != -1) {
path.write(buf);
path.flush();
}
}
What the code does is, it only write the content of the Originalfile in the first-copied file and the following files are empty
EDIT:
To clarify what the code should do is write the first 8 bytes to go to file 1, second 8 bytes to go to file 2, third 8 bytes to go to file 3, fourth 8 bytes to go to file 1, and so on, round robin, until file 1 is sizeOfParts[0] long, file 2 is sizeOfParts[1] long, and file 3 is sizeOfParts[2] long.
The main problem is that the readWrite() method is only supposed to copy one 8-byte block of bytes, but has a loop that makes it copy all the remaining bytes in the input file.
In addition, the code should be enhanced to use try-finally to close the files, and to correctly handle end-of-file, in case the input file is shorter than the sum of parts.
I would eliminate the readWrite() method, and consolidate the logic to prevent duplicate code, like this:
public void splitFile(String inPath, long[] sizeOfParts) throws IOException, SQLException {
final int numberOfParts = sizeOfParts.length;
String[] outPath = new String[numberOfParts];
// Gets Paths from Database here
InputStream in = null;
OutputStream[] out = new OutputStream[numberOfParts];
try {
in = new BufferedInputStream(new FileInputStream(inPath));
for (int part = 0; part < numberOfParts; part++)
out[part] = new BufferedOutputStream(new FileOutputStream(outPath[part]));
byte[] buf = new byte[8];
long[] remain = sizeOfParts.clone();
for (boolean done = false; ! done; ) {
done = true;
for (int part = 0; part < numberOfParts; part++) {
if (remain[part] > 0) {
int len = in.read(buf, 0, (int) Math.min(remain[part], buf.length));
if (len == -1) {
done = true;
break;
}
remain[part] -= len;
System.out.println("file " + part + " " + (sizeOfParts[part] - remain[part]));
out[part].write(buf, 0, len);
done = false;
}
}
}
} finally {
if (in != null)
in.close();
for (int part = 0; part < out.length; part++)
if (out[part] != null)
out[part].close();
}
}

Java client to C# server TCP

The problem is when i send up to 40 KB everything is okay when i send more sometime half of the data received some time nothing ,is there a limit of the networkstream.Read ,even though i cunked the data ,i can't determine if the problem form the java or the c# from the network stream or the Output stream
C# SERVER
private void ReadData(){
if (networkStream.DataAvailable)
{
int size = GetBufferSize();
Thread.Sleep(340);
byte[] myReadBuffer = new byte[size];
int numberOfBytesRead = 0;
while (true)
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
if (numberOfBytesRead >= myReadBuffer.Length)
{
break;
}
}
string str = Encoding.UTF8.GetString(myReadBuffer, 0, size);
dynamic Message = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(str);
// Android Message , JSON String
if (OnAndroidMessage != null)
{
OnAndroidMessage(Message);
}
}
}
private int GetBufferSize()
{
byte[] myReadBuffer = new byte[4];
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
} while (networkStream.DataAvailable && numberOfBytesRead < myReadBuffer.Length);
if (numberOfBytesRead > 0)
{
// reverse the byte array.
if (BitConverter.IsLittleEndian)
{
Array.Reverse(myReadBuffer);
}
return BitConverter.ToInt32(myReadBuffer, 0);
}
else
{
return 0;
}
}
Java Client // i tested this also without cutting the data to smaller paces ,half of the data received not all of them
mBufferOut = socket.getOutputStream();
private void sendMessage(final String message) {
if (mBufferOut != null && message != null) {
try {
byte[] data = message.getBytes("UTF-8");
Log.d("_TAG", "Sending: " + message);
Log.d("_TAG", "Message length: " + Integer.toString(data.length));
mBufferOut.write(toByteArray(data.length));
mBufferOut.flush();
List<byte[]> divideArray = divideArray(data, 10000);
for (byte[] dataChunk : divideArray) {
Log.e("_TAG","CHUNK SIZE > " + Integer.toString(dataChunk.length));
mBufferOut.write(dataChunk, 0, dataChunk.length);
mBufferOut.flush();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
private List<byte[]> divideArray(byte[] source, int chunksize) {
List<byte[]> result = new ArrayList<byte[]>();
int start = 0;
while (start < source.length) {
int end = Math.min(source.length, start + chunksize);
result.add(Arrays.copyOfRange(source, start, end));
start += chunksize;
}
return result;
}
Any ideas ?
Solution from this post NetworkStream is reading data that should not be there
static void ReadExact(Stream stream, byte[] buffer, int offset, int count)
{
int read;
while(count > 0 && (read = stream.Read(buffer, offset, count)) > 0) {
offset += read;
count -= read;
}
if(count != 0) throw new EndOfStreamException();
}
the problem is the Read it takes size and want to get that size you need to give it chunks and check each chunk
And also read does not restart from where it stopped until it reads the amount is set to read meaning if i set to read 10 then if it not find the 10 then it will read what it find as example it reads 6 ,it will return 6 and when to loop another time ti read the rest it dose not start from 6 it start from 0 and read until 4 so you overwrite your data ,and if it read 10 from the first try then it set the read to finish so it dose not start from 0 ,it needs to read the amount the has been set to it to re set the read to new buffer location.

how to get a maximum values of two peaks of an array and take them away using for loops? any ideas?

how to get a maximum value of two peaks of an array and take them away using for loops? any ideas?
was thinking to use 2 for loops to find the values in the array. i am using an acceloremeter and displaying the result within a graph but now i need to find the 2 peaks and take them away to determine the outcome and display it.
SM.unregisterListener(this);
File path = getApplicationContext().getExternalFilesDir(null);
File file = new File(path, "my_file-name.txt");
// String filename = "my_file";
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file); //openFileOutput(file, Context.MODE_APPEND);
for (double d : array) {
String s = Double.toString(d) + ",";
outputStream.write(s.getBytes());
}
String newline = "/n";
outputStream.write(newline.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
this code values are stored within a file so i can then display it within a graph
Here is how you can find two peaks in one loop:
File file = new File(path, "my_file-name.txt");
// String filename = "my_file";
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file); //openFileOutput(file, Context.MODE_APPEND);
double peak1, peak2;
if(array.length >= 2) {
peak1 = array[0];
peak2 = array[1];
} else { // not enough elements
return;
}
for (double d : array) {
// peak2 is greater, leave it;
// save new value to peak1 ?
if(peak1 < peak2 && d > peak1) {
peak1 = d;
} else if(d > peak2) { // peak1 is greater or d is less
peak2 = d;
}
String s = Double.toString(d) + ",";
outputStream.write(s.getBytes());
}
String newline = "/n";
outputStream.write(newline.getBytes());
outputStream.close();
System.out.println("Peaks: " + peak1 + " ; " + peak2);
} catch (Exception e) {
System.out.println("Error: " + e);
//e.printStackTrace();
}
See code sample here.
private void stopSensor() {
SM.unregisterListener(this);
File path = getApplicationContext().getExternalFilesDir(null);
File file = new File(path, "my_file-name.txt");
// String filename = "my_file";
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file); //openFileOutput(file, Context.MODE_APPEND);
for (double d : array) {
String s = Double.toString(d) + ",";
outputStream.write(s.getBytes());
}
String newline = "/n";
outputStream.write(newline.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
double peak1, peak2;
int peaklocation1, peaklocation2;
if (array.length >= 2) {
peak1 = array[0];
peak2 = array[1];
peaklocation1 = 0;
peaklocation2 = 1;
} else { // not enough elements
return;
}
for (int i = 0; i < array.length; i++) {
double d = array[i];
// peak2 is greater, leave it;
// save new value to peak1 ?
if (peak1 < peak2 && d > peak1) {
peak1 = d;
peaklocation1 = i;
} else if (d > peak2) { // peak1 is greater or d is less
peak2 = d;
peaklocation2 = i;
}
}
int size = peaklocation1;
size = peaklocation1 - peaklocation2 ;
resultText.setText("Result:" + peaklocation1 );
resultText2.setText("Result:" + peaklocation2);
} catch (Exception e) {
// System.out.println("Error: " + e);
//e.printStackTrace();
}

Categories