×
Namespaces

Variants
Actions
(Difference between revisions)

Converting a resource into a byte array in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
+
{{KBCS}}
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
|-
 
|-
|'''ID''' ||  
+
|'''ID''' || CS000979
|'''Creation date''' || May 21, 2008
+
|'''Creation date''' || May 27, 2008
 
|-
 
|-
 
|'''Platform''' || S60 3rd Edition, FP1
 
|'''Platform''' || S60 3rd Edition, FP1
Line 24: Line 24:
 
This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the [[JAR]] file, which is used to distribute the application.
 
This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the [[JAR]] file, which is used to distribute the application.
  
'''Example''': To include a file to the JAR package in [http://www.netbeans.org/ NetBeans 6.1], follow these instructions:
+
'''Example''': To include a file to the JAR package in [http://www.netbeans.org/ NetBeans 6.1], do as follows:
  
# In the <tt>Projects</tt> window (select <tt>Window</tt> &gt; <tt>Projects</tt> if it's not visible), open the project node for the project to which you want to add the file.
+
# In the '''Projects''' window (select '''Window''' &gt; '''Projects''' if it's not visible), open the project node for the project to which you want to add the file.
# Right-click on the <tt>Resources</tt> node under the project and choose <tt>Add Folder</tt>. The Add Folder dialog box opens.
+
# Right-click on the '''Resources''' node under the project and choose '''Add Folder'''. The Add Folder dialog box opens.
# Select the folder from which the resource can be found (e.g. <tt>dist/images</tt>).
+
# Select the folder where the resource is (for example, <tt>dist/images</tt>).
# Click <tt>Open</tt>. The folder is added to the project.
+
# Click '''Open'''. The folder is added to the project.
# Clean and build the project (right-click on the project node and select <tt>Clean &amp; Build</tt>).
+
# Clean and build the project (right-click on the project node and select '''Clean &amp; Build''').
# The resource can be obtained in the code for example like this:
+
# The resource can be obtained in the code, for example, like this:
  
 
<code java>
 
<code java>
Line 60: Line 60:
 
     byte[] bytes = new byte[512];
 
     byte[] bytes = new byte[512];
  
     // Read bytes from the input stream in bytes.length -sized chunks and write
+
     // Read bytes from the input stream in bytes.length-sized chunks and write
 
     // them into the output stream
 
     // them into the output stream
 
     int readBytes;
 
     int readBytes;
Line 78: Line 78:
 
</code>
 
</code>
  
The method above can be used for example with multipart messages. In the following, a <tt>MessagePart</tt> object is constructed from a byte array which represents raw data from a JPEG image:
+
The method above can be used, for example, with multipart messages. In the following, a <tt>MessagePart</tt> object is constructed from a byte array which represents raw data from a JPEG image:
  
 
<code java>
 
<code java>
Line 114: Line 114:
 
==See also==
 
==See also==
  
* [[Sending a multipart MMS]]
+
* [[CS000979 - Sending a multipart MMS]]
  
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Examples]][[Category:WMA 2.0 (JSR-205)]]
+
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Examples]][[Category:WMA 2.0 (JSR-205)]][[Category:S60 3rd Edition, Feature Pack 1]]

Revision as of 10:02, 27 May 2008


Template:KBCS

ID CS000979 Creation date May 27, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95 8GB
Category Java ME Subcategory WMA 2.0 (JSR-205)


Keywords (APIs, classes, methods, functions): java.lang.Class, java.io.InputStream, java.io.ByteArrayOutputStream, java.io.IOException, java.lang.Class.getResourceAsStream(), java.io.InputStream.read(), java.io.ByteArrayOutputStream.write(), java.io.ByteArrayOutputStream.toByteArray(), java.io.InputStream.close(), java.io.ByteArrayOutputStream.close()

Overview

This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the JAR file, which is used to distribute the application.

Example: To include a file to the JAR package in NetBeans 6.1, do as follows:

  1. In the Projects window (select Window > Projects if it's not visible), open the project node for the project to which you want to add the file.
  2. Right-click on the Resources node under the project and choose Add Folder. The Add Folder dialog box opens.
  3. Select the folder where the resource is (for example, dist/images).
  4. Click Open. The folder is added to the project.
  5. Clean and build the project (right-click on the project node and select Clean & Build).
  6. The resource can be obtained in the code, for example, like this:
InputStream inputStream = getClass().getResourceAsStream("image.jpg");
// ...

Source

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Constructs a byte array and fills it with data that is read from the
* specified resource.
* @param filename the path to the resource
* @return the specified resource as a byte array
* @throws java.io.IOException if the resource cannot be read, or the
* bytes cannot be written, or the streams cannot be closed
*/

private byte[] obtainByteData(String filename) throws IOException {
InputStream inputStream = getClass().getResourceAsStream(filename);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
byte[] bytes = new byte[512];
 
// Read bytes from the input stream in bytes.length-sized chunks and write
// them into the output stream
int readBytes;
while ((readBytes = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, readBytes);
}
 
// Convert the contents of the output stream into a byte array
byte[] byteData = outputStream.toByteArray();
 
// Close the streams
inputStream.close();
outputStream.close();
 
return byteData;
}

The method above can be used, for example, with multipart messages. In the following, a MessagePart object is constructed from a byte array which represents raw data from a JPEG image:

import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.SizeExceededException;
 
// ...
 
/**
* Constructs a MessagePart which can be added to a MultipartMessage.
* @return the constructed MessagePart
* @throws javax.wireless.messaging.SizeExceededException if the contents
* is larger than the available memory or supported size for the message
* part
* @throws java.io.IOException if the byte data cannot be obtained
*/

private MessagePart createMsgPart() throws SizeExceededException,
IOException {
String imageContentID = "image01";
String imageContentLocation = "image.jpg";
String jpgMIME = "image/jpeg";
// Convert the image into a byte array and construct a message part using
// it
byte[] imageContent = obtainByteData(imageContentLocation);
MessagePart messagePart = new MessagePart(imageContent, jpgMIME,
imageContentID, imageContentLocation, null);
return messagePart;
}

Postconditions

A specified resource is converted into a byte array.

See also

260 page views in the last 30 days.