I m trying to get/convert the value of Arraylist in byte[]
below is my code
final ArrayList<Object> imglists = new ArrayList<Object>();
this is my arraylist of Objects in this arraylist m storing the values of images in form of bytes
for (int i=0; i<mPlaylistVideos.size();i++) {
holder.mThumbnailImage.buildDrawingCache();
Bitmap bitmap= holder.mThumbnailImage.getDrawingCache();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bs);
byte[] rough = bs.toByteArray();
imglists.add(i,rough);
}
I m trying to get the specific value from arraylist and store that in byte[]
this is what I was trying to do
byte[] value=imglists.get(2);
I could not find any complete answer to convert Arraylist of Object into byte[]
I know Arraylist doesn't support primitive datatype (i-e byte)
What you are looking for is a List of byte[], something like that:
List<byte[]> imglists = new ArrayList<>();
Then you can simply add your byte array to your List using the add(E) method as next:
imglists.add(bs.toByteArray());
You will then be able to access to a given byte array from its index in the List using the method get(int) as you try to achieve:
// Get the 3th element of my list
byte[] value = imglists.get(2);
You want to convert ArrayList to byte[] ? or Object to byte[]?
I wrote in this way, just simply convert the element in ArrayList into byte[] ,it works!
List<Object> objects = new ArrayList<Object>();
objects.add("HelloWorld".getBytes());
byte[] bytes = (byte[]) objects.get(0);
System.out.println(new String(bytes)); // HelloWorld
Related
All I'm trying to do is to convert my java List to BLOB and vice-versa. I have successfully done the first part where I have converted my List and stored it as BLOB in DB. However, I'm to get the BLOB but unable to convert it back to List. I have tried searching at many places but not able to figure out a way to solve this problem I'm facing. Hope I'll get a solution here.
Converting the List and storing it as BLOB in DB:
List<CustomerDTO> customersList = new ArrayList<CustomerDTO>();
.
.
some code to store the data in to List<CustomerDTO>
.
.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(customersList);
byte[] bytes = baos.toByteArray();
// set parameters
pstmt.setBinaryStream(1, new ByteArrayInputStream(bytes));
pstmt.executeUpdate();
Failed attempts of converting BLOB to List:
1)
Blob blob = rs.getBlob("CUSTOMERS_LIST");
byte[] bdata = blob.getBytes(1, (int) blob.length());
ByteArrayInputStream bais = new ByteArrayInputStream(bdata);
ObjectInputStream ois = new ObjectInputStream(bais);
List<CustomerDTO> list = (ArrayList<CustomerDTO>) ois.readObject();
This attempt has given me empty list
2)
Stream<T> stream = (Stream<T>) rs.getBinaryStream("CUSTOMERS_LIST");
List<CustomerDTO> list = (List<CustomerDTO>) stream.collect(Collectors.toList());
This attempt thrown me error java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be cast to java.util.stream.Stream
3)
IntStream bais = (IntStream) rs.getBinaryStream("CUSTOMERS_LIST");
Stream<Integer> stream = (Stream<Integer>) bais.boxed().collect(Collectors.toList());
List<Integer> ll = (List<Integer>)stream.collect(Collectors.toList());
I'm lost here as I don't get how to convert List<Integer> to List<CustomerDTO>
Looking forward for your help.. TIA!
I have modified your first attempt slightly as follows:
Blob blob = rs.getBlob("CUSTOMERS_LIST");
byte[] bdata = blob.getBytes(1, (int) blob.length());
ArrayList<Object> arraylist = new ArrayList<>();
for(byte b : bdata) {
arraylist.add(new Byte(b));
}
I have a binary file, in my jar, and I want to slurp its contents in binary mode, not into a string of characters. Following this example
private byte[] readBinaryFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int read = input.read(); read >= 0; read = input.read())
output.write(read);
byte[] buffer = output.toByteArray();
input.close ();
output.close();
return buffer;
}
It's pretty trivial, but the calling context is expecting and Object. How do I pass this binary contents back to the caller, but not as a primitive array? I am trying to deliver this binary data as a response to a web service using jaxrs.
As #Jon notes, the caller should be just fine:
byte[] b = new byte[10];
Object o = b;
That works because as he points out a byte[] is an instance of Object.
Don't confuse bytes themselves, which are indeed primitives, with the array. All arrays are objects no matter what they contain.
So the caller should receive his Object and then send it back to his caller as application/octet-stream.
I have a arraylist of Bytes and i am converting them into a byte array.I have used the following method.However it gives me the following error:
E/AndroidRuntime(5228): java.lang.NoClassDefFoundError: com.google.common.primitives.Bytes
ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
byte[] bytes_song_byte;
for(int i=0;i<int_arraylist.size();i++)
{
bytes_song_byte=Bytes.toArray(byteArrayList_song);
}
Looks like Google Guava is not on your class path, also you should remove the for loop from the above code, as that is what the Guava function does for you.
ArrayList<Byte> byteArrayList_song = new ArrayList<Byte>();
byte[] bytes_song_byte = Bytes.toArray(byteArrayList_song);
You can do this conversion without external libs
byte[] bytes_song_byte = new byte[byteArrayList_song.size()];
for (int i = 0; i < byteArrayList_song.size(); i++) {
bytes_song_byte[i] = byteArrayList_song.get(i);
}
note that if byteArrayList_song has any null elements this code will throw a NullPointerException
Try the following
ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
byte[] bytes_song_byte;
bytes_song_byte=byteArrayList_song.toArray(new Byte[byteArrayList_song.size()]);
I have a web service that I am re-writing from VB to a Java servlet. In the web service, I want to extract the body entity set on the client-side as such:
StringEntity stringEntity = new StringEntity(xml, HTTP.UTF_8);
stringEntity.setContentType("application/xml");
httppost.setEntity(stringEntity);
In the VB web service, I get this data by using:
Dim objReader As System.IO.StreamReader
objReader = New System.IO.StreamReader(Request.InputStream)
Dim strXML As String = objReader.ReadToEnd
and this works great. But I am looking for the equivalent in Java.
I have tried this:
ServletInputStream dataStream = req.getInputStream();
byte[] data = new byte[dataStream.toString().length()];
dataStream.read(data);
but all it gets me is an unintelligible string:
data = [B#68514fec
Please advise.
You need to use a ByteArrayOutputStream, like this:
ServletInputStream dataStream = req.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int r;
byte[] buffer = new byte[1024*1024];
while ((r = dataStream.read(data, 0, buffer.length)) != -1) {
baos.write(buffer, 0, r);
}
baos.flush();
byte[] data = baos.toByteArray();
You are confusing with printing of java arrays. When you print any java object it is transformed to its string representation by implicit invocation of toString() method. Array is an object too and its toString() implementation is not too user friendly: it creates string that contains [, then symbolic type definition (B for byte in your case, then the internal reference to the array.
If you want to print the array content use Arrays.toString(yourArray). This static method creates user-friendly string representation of array. This is what you need here.
And yet another note. You do not read your array correctly. Please take a look on #Petter`s answer (+1) - you have to implement a loop to read all bytes from the stream.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Creating a byte[] from a List<Byte>
I have List list. How to get byte[] ( subarray of list ) from startIndex to endIndex id list ?
List<Byte> theList= new ArrayList<Byte>();
Byte[] your_bytes = theList.subList(startIndex,endIndex).toArray(new Byte[0]);
If finally you need to work with byte (the primitive) then I recommend Apache Commons Collections toPrimitive utility
byte[] your_primitive_bytes = ArrayUtils.toPrimitive(your_bytes);
For most cases you certainly can get by with Byte (object).
ArrayList<Byte> list = new ArrayList<Byte>();
ArrayList<Byte> subList = (ArrayList<Byte>) list.subList(fromIndex, toIndex); //(0,5)
Byte[] array = (Byte[]) subList.toArray();
Well, since the original question actually asks for a sublist containing a byte[] (not Byte[]) here goes:
List<Byte> byteList = .... some pre-populated list
int start = 5;
int end = 10;
byte[] bytes = new byte[end-start]; // OP explicitly asks for byte[] (unless it's a typo)
for (int i = start; i < end; i++) {
bytes[i-start] = byteList.get(i).byteValue();
}
If you need byte[]:
byte[] byteArray = ArrayUtils.toPrimitive(list.subList(startIndex, endIndex).toArray(new Byte[0]));
ArrayUtils.toPrimitive