Showing posts with label byte array. Show all posts
Showing posts with label byte array. Show all posts

Tuesday, 17 May 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:
/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, 
length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 
offset, length);
int ch;

while ((ch = bis.read()) != -1)
{
System.out.print((char)ch);
}

}



Using the above function:

String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

Thursday, 12 May 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:

/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, offset, length);
int ch;

while ((ch = bis.read()) != -1)
{
System.out.print((char)ch);
}

}




Using the above function:



String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

Read byte array using ByteArrayInputStream Example

Here we read the byte array and put it on console.

Function to read the byte array:

public static void readByteArray(byte[] bytes){
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);

int ch;

//read bytes from ByteArrayInputStream using read method
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
}


}



Using the above function:



String str = "ByteArrayInputStream Example!";

//get bytes from string using getBytes method
byte[] bytes = str.getBytes();
readByteArray(bytes);



For partial reading of byte array read here.

Wednesday, 11 May 2011

Write byte array to file using BufferedOutputStream

Writing the function:

public static void writeByteArrayToFile(String fileName, byte[] byteArr)
{
BufferedOutputStream bos = null;
try
{
//create an object of FileOutputStream
FileOutputStream fos = new FileOutputStream(new File(strFileName));
//create an object of BufferedOutputStream
bos = new BufferedOutputStream(fos);


/*
* To write byte array to file use,
* public void write(byte[] b) method of BufferedOutputStream
* class.
*/
System.out.println("Writing byte array to file");
bos.write(byteArr);
System.out.println("File written");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing file" + ioe);
}
finally
{
if(bos != null){
try
{
//flush the BufferedOutputStream
bos.flush();
//close the BufferedOutputStream
bos.close();
}
catch(Exception e){//don't swallow
//exceptions..add your code accordingly
}
}
}
}


Using the above function:


String fileName = "C:\demo.txt";
String str = "BufferedOutputStream Example to write byte array";
writeByteArrayToFile(fileName,str.getBytes());

Monday, 25 April 2011

Turn String into InputStream

import java.io.*;

public InputStream stringToStream(String s) {
return new ByteArrayInputStream(s.getBytes());
}

Java read file as utf8

Specify "UTF-8" as charsetName to the constructor of InputStreamReader. From JavaDoc:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
BufferedReader r = new BufferedReader(
new InputStreamReader(
new FileInputStream("com/demo/io/test.txt"),
"UTF-8"
)
);
String l = r.readLine();
while (l != null) {
System.out.println(l);
l = r.readLine();
}
r.close();

Java read file as byte array

import java.io.*;

public class ByteFileReader {
public static void main(String[] args) throws Exception {
String fileName = "com/demo/io/test.txt";
ByteFileReader r = new ByteFileReader();
byte[] bytes = r.getBytesFromFile(new File(fileName));
System.out.printf("Read %d bytes from file %s:\n", bytes.length, fileName);
System.out.write(bytes, 0, bytes.length);
}

public byte[] getBytesFromFile(File file) throws IOException {
if (! file.exists()) {
return null;
}

// get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new IOException(
String.format(
"The file %s is too large to be hold in a byte array",
file.getName()
)
);
}

int len = (int) length;
byte[] bytes = new byte[len];

InputStream in = new FileInputStream(file);

// read in the bytes
int offset = 0, n = 0;
while (offset < len && n >= 0) {
n = in.read(bytes, offset, len - offset);
offset += n;
}

if (offset < len) {
throw new IOException("Faile to read all contents of file: " + file.getName());
}

in.close();
return bytes;
}
}