Count appended files / output Stream JAVA - java

I have the following OutputStream which saves SQL receiving BLOB to a network drive. The boolean "true" affects that files which are already there dont get overwritten.
In conclusion, if there are 500 BLOB's in the buffer but 200 of them already saved to the network drive before, only 300 new files will be added.
Question: How could I count the new files?
I want to write a them to a logfile.
Thanks in advance!
OutputStream out = new FileOutputStream(path + "\\" + xy +".jpg", true);
byte[] buff = blob.getBytes(1, (int) blob.length());
out.write(buff);
out.close();

Related

How to save the Picture content to Neo4J Database? About Java IO

I am a freshman in Neo4J. I think I am also a freshman in Java though I have learn it for neary 2 years.
I want to save and read a picture in neo4j database, I have a InputStream instance, Its cotent is a picture data. I have a Resoucre Object. it has a byte[] property used to save the picture data. so I do that
public static Resource getResourceInstance(InputStream in, String title) throws IOException{
StringBuilder sb = new StringBuilder();
BufferedInputStream input = new BufferedInputStream(in);
int b;
while((b = input.read()) != -1){
sb.append(b);
}
input.close();
in.close();
return new Resource(sb.toString().getBytes(), title, 0, 0);
}
then I use a transaction to save it to neo4j. and I check it by neo4j-server. in database, the byte array is number like 51,52,45 and so on
the second step I want to read the byte array from database.
I put it in Resource Object. and use FileOutputStream read it the code like this
images = resource.getImage();
String titleString = resource.getTitle();
String path = "images" + File.separator + titleString + ".jpg";
System.out.println(Paths.get(path).toRealPath());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(path)));
out.write(images);
out.close();
this is a Java web project.
I don't know why I have to create a file in path(String path = "images" + File.separator + titleString + ".jpg";) at first.
though I do so, I can't open the file like a picture.
I am very dispirited now. and I don't konw how to do. can you help me?
thank you very much.
PS:
my english is poor,bet your tolerating.
don't do this in the first place.
store the picture on a filesystem or a public storage like s3, dropbox etc. and save the url or filename in the neo4j property.
If you want to read a file into a byte[] create an array of the file.length() size and read into that array using the right offset until is. read() returns -1

android tcp client file receive

I am trying to send a file (png to be specific) over sockets from python server to android client. I know that my python server is sending the data, I just can't figure out how to receive the data on the android side. Here is what the code looks like to receive the file.
String path = Environment.getExternalStorageDirectory().toString() +"/tmp/test.png";
try {
socket = new Socket("192.168.1.129", 29877);
is = socket.getInputStream();
out = new FileOutputStream(path);
byte[] temp = new byte[1024];
for(int c = is.read(temp,0,1024); c > 0; c = is.read(temp,0,1024)){
out.write(temp,0,c);
Log.d("debug tag", out.toString());
}
Log.d("debug tag", temp.toString());
Bitmap myBitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
imageView.setImageBitmap(myBitmap);
Thanks for any advice.
You are reading from socket in 1K chunks and saving them into a file. Then you try to interpret the last chunk as a bitmap. This doesn't work.
Either read your image from the file after you saved it, or buffer it all in memory.

java program download file is corrupt. Why?

I have created a java program that downloads a file from a URL part by part into several files, then reads the bytes from those files into the full downloaded object. It works by separating sections of the file to be downloaded into threads. Every time my program downloads a file it gets all of the bytes and the file size is correct, but sometimes with an image the picture is distorted. Other times the image is perfect. What would cause this?
code that individual threads use to download file parts:
URL xyz = new URL(urlStr);
URLConnection connection= xyz.openConnection();
// set the download range
connection.setRequestProperty("Range", "bytes="+fileOffset+"-");
connection.setDoInput(true);
connection.setDoOutput(true);
// set input stream and output stream
in = new BufferedInputStream(connection.getInputStream());
fos = new FileOutputStream("part_"+this.partNumber);
out = new BufferedOutputStream(fos, this.downloadFileSize);
// create buffer to read bytes from file into
byte[] contentBytes = new byte[downloadFileSize];
// read contents into buffer
in.read(contentBytes, 0, this.downloadFileSize);
out.write(contentBytes, 0, this.downloadFileSize);
code that puts file together:
int partSize=0;
//Create output stream
OutputStream saveAs = new FileOutputStream(fileName);
for(int i=0; i<filePieces;i++)
{
File file=new File("part_"+(i+1));
partSize=(int)file.length();
byte fileBuffer[]=new byte [partSize];
//Create input stream
InputStream is = new FileInputStream(file);
is.read(fileBuffer);
saveAs.write(fileBuffer);
is.close();
}
Without further details and sample code you're forcing any answers to be guesses. Here are mine:
You're using Readers and Writers when you should use Input- / OutputStreams.
You've messed up the synchronization somehow. Favour classes from the java.util.concurrent package over home grown synchronized solutions.

Program returning "could not get audio input stream from input file" for specific .WAV file

I have a program that involves the analysis and output of .wav files. It works perfectly fine with tracks from cds or the internet but I have generated a sequence of pure sine wave tones using matlab to analyse and it is giving me the error shown in the title. The matlab files run fine in iTunes so I'm not sure why my program is having trouble with it.
public static void signalToFile(File f) throws IOException, UnsupportedAudioFileException
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(f);
int numBytes = inputStream.available();
byte[] buffer = new byte[numBytes];
inputStream.read(buffer, 0, numBytes);
String newFile = f.getName().replace(".wav", ".txt");
System.out.println("Beginning file write: " + newFile + " (soundUtilities)");
BufferedWriter fileOut = new BufferedWriter(new FileWriter(new File("src/examples/Media/" + newFile)));
System.out.println("Ending file write: " + newFile + " (soundUtilities)");
System.out.println(buffer.length);
ByteBuffer myBB = ByteBuffer.wrap(buffer);
myBB.order(ByteOrder.LITTLE_ENDIAN);
while(myBB.remaining() > 1)
{
short current = myBB.getShort();
fileOut.write(String.valueOf(current));
fileOut.newLine();
}
fileOut.flush();
fileOut.close();
inputStream.close();
}
The first line of this method is what is causing the error. This method involves the signal information for the file to a txt file. Any help would be greatly appreciated.
Below is the small section of matlab code I used to create the wav file:
x13 = sin(2*pi*220*t1); % A3 long sample
x13envelope = [1:-1/length(x13):1/length(x13)];
x13full = x13.*x13envelope;
totalSound = [x1full x2full x3full x4full x5full x6full x7full x8full x9full x10full x11full x12full x13full]; % combines the notes
wavwrite(totalSound, fs, 32, 'TestTune');
Each of the entries in the "totalSound" array represent a note
WAV files have a header. You don't seem to create that header. What you seem to create is a raw PCM file.
I exepriences this problem "The output is different in JBoss 4.2 and JBoss 7
JBoss 4.2:
Support WAVE = true
JBoss 7:
Support WAVE = false"
check also this https://community.jboss.org/message/729654

Java: resume Download in URLConnection

I wrote a program that downloads some files from some servers.
Currently program works properly.
But I want to add resume support to it.
I'm doing it like this But the result file is corrupted:
....
File fcheck=new File(SaveDir+"/"+filename);
if(resumebox.isSelected() && fcheck.exists()){
connection.setRequestProperty("Range", "Bytes="+(fcheck.length())+"-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
BufferedInputStream in = new BufferedInputStream (connection.getInputStream());
pbar.setIndeterminate(false);
pbar.setStringPainted(true);
java.io.FileOutputStream fos ;
if(resumebox.isSelected()){
if(fcheck.exists()){
if(connection.getHeaderField("Accept-Ranges").equals("bytes")){
fos = new java.io.FileOutputStream(SaveDir+"/"+filename,true);
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
....
I'm Testing it on a server that I know supports resume.
I downloaded some bytes.(72720)
Then Tried to resume it.
Then I opened file with a Hex editor , At offset 72720 the first Bytes are repeated:
Bytes 0-36: FLV.............«..........onMetaData
Bytes 72720-72756: FLV.............«..........onMetaData
It Starts download from the begining!
While when I do it by wget it does correctly and responses by Content-Range field!
Server responses with "302 FOUND" and a "206 Partial Content" in wget log.
Can "302 FOUND" cause the problem?
What is the problem ?
Thanks.
Try:
connection.setRequestProperty("Range", "bytes=" + fcheck.length() + "-");
Lowercase the range specifier per the spec. Also, if your partial file was 500 bytes, that means your byte range that you have is 0-499, and you want 500+.
The problem is in (fcheck.length() - 1): this should be fcheck.length().

Categories