IOUtils.copy throws IOException for files larger than ~1 mb - google-app-engine

I'm using Apache Commons IO's IOUtils.copy to make copies of existing files, on Google Cloud Storage.
Here is the problem: if the files goes beyond around 1 mb, IOUtils.copy will throw an exception. Files below 1 mb works perfectly fine.
Code snippet
AppEngineFile newFile = null;
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder().setBucket("bucket-name")
.setKey(newFilename).setMimeType("image/jpeg");
try {
//currentFile is an instance of a AppEngineFile
FileReadChannel readChannel = fileService.openReadChannel(currentFile, true);
newFile = fileService.createNewGSFile(optionsBuilder.build());
FileWriteChannel writeChannel = fileService.openWriteChannel(newFile, true);
InputStream inputStream = Channels.newInputStream(readChannel);
OutputStream outputStream = Channels.newOutputStream(writeChannel);
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
outputStream.close();
inputStream.close();
writeChannel.closeFinally();
} catch (IOException e) {
log.warning(e.toString());
}
I also attempted a more primitive method: but it doesn't work too
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
while (len != -1) {
outputStream.write(buffer, 0, len);
len = inputStream.read(buffer);
}
My assumption now: there is some kind of restriction on the App Engine platform.
Note: I've also tried IOUtiles.copyLarge (which is meant for files larger than 2 GB), but it doesn't work too.

Try using a smaller than 1mb buffer. Latest appengine version reduced buffer size.

Related

Xamarin.android - Copy .jpg to shared folder

I'm using a Samba File Server for copy files from my Android device to a shared folder on Windows. With .txt files i haven't any problems, works fine, but I tried to copy a .jpg file into shared folder and it fails. I searched a lot of codes from internet but anyone solved my problem.
I managed to copy the image, but when I open it, is damaged.
Does anyone have any sample code?
My code is this:
Java.IO.File mfile = new Java.IO.File(item.FullName);
var mSharedFolderCalidad = new SmbFile(AppGlobalConfigSoftware.Instance.Parameters.PathToDownloadCalidad + item.Name);
//If exists don't create another time
if (!mSharedFolderCalidad.Exists())
mSharedFolderCalidad.CreateNewFile();
InputStream inFile = new FileInputStream(mfile);
SmbFileOutputStream sfos = new SmbFileOutputStream(mSharedFolderCalidad);
byte[] buf = new byte[1024];
int len;
while ((len = inFile.Read(buf)) > 0)
{
sfos.Write(buf, 0, len);
}
inFile.Close();
sfos.Close();
All help is appreciated.
Thank you.
You can use Media.Plugin from nuget to take photo firstly.
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
PhotoSize = PhotoSize.Medium,
});
public byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
MediaFile has GetStream().
You could use this to the Stream and then convert that to a byte[]. Here is one way to do that:
Define a stream
Stream imageStream;
And init it after you take the photo .
imageStream = file.GetStream();
var imageArr= ReadFully(imageStream );
And then write it to your folder .

Java - Load file as Template + append content in memory and manage byte[]

Im trying load a file in memory with a base information, append lines and include the result into a Zip file. In C# existes MemoryStream but, in java not.
the context of my application is load a stylesheet.css files with a pre-defined styles for add other styles that i get dinamically. Later i want add this content to a zip entry, and i need a byte[] that represent this content.
For the moment, i have the next lines, but i dont know as convert this to byte[]:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("style.css").getFile());
OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( file ) );
BufferedWriter writer = new BufferedWriter(osw);
I tried, with ByteArrayOutputStream but i can't completed all my requirements.
Any idea? im opne to other ideas for get my goal. I looking for CSSParser too, but i didn't see as I can append content and get a File document (byte[]) for to add to my zip file.
Finnaly, i didn't find other solution for my problem that convert the InputStream to ByteArrayOutputStream byte to byte.
I created the following methods:
Load template file into Input Stream and convert.
private ByteArrayOutputStream getByteArrayOutputStreamFor(String templateName) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inStream = classLoader.getResourceAsStream( templateName ); //file into resources directory
ByteArrayOutputStream baos = Utils.toOutputStream(inStream);
return baos;
} catch (Exception e) {
String msg = String.format("erro to loaf filetemplate {%s}: %s", templateName, e.getMessage());
throw new RuntimeException( msg, e.getCause() );
}
}
Copy the inputStream into a ByteArrayOutputStream byte to byte
public static final ByteArrayOutputStream toOutputStream(InputStream inStream) {
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int byteReads;
while ((byteReads = inStream.read()) != -1) {
outStream.write(byteReads);
}
outStream.flush();
inStream.close();
return outStream;
} catch (Exception e) {
throw new RuntimeException("error message");
}
}
Finally, I append text to ByteArrayOutputStream
ByteArrayOutputStream baosCSS = getByteArrayOutputStreamFor( "templateName.css" );
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( baosCSS ) );
writer.append( "any text" );
writer.flush();
writer.close();
byte[] bytes = baosCSS.toByteArray()

save a photo with a specific name and compressed size in codenameone

I want to take a photo and save it with a specific name and with compressed size (low quality). How do I do it? The image is getting saved with some random name on Android device and simulator.
String filePath = Capture.capturePhoto();
if(filePath != null) {
try {
ImageIO io = ImageIO.getImageIO();
io.saveAndKeepAspect(filePath,
FileSystemStorage.getInstance().getAppHomePath()+"testImage.jpg",
ImageIO.FORMAT_JPEG, 100, 100, 0.1f, true, false);
InputStream inputStream = FileSystemStorage.getInstance().openInputStream(FileSystemStorage.getInstance().
getAppHomePath()+"testImage.jpg");
Image readImage = Image.createImage(inputStream);
Util.cleanup(inputStream);
imgViewer.setImage(readImage);
} catch(IOException err) {
err.printStackTrace();
}
hi.add(imgViewer);
hi.show();
I have done something similar, but i used the Storage, altough i added a comment with FileSystemStorage for you.
These lines create the files under the name i declare in the outputStream.
//OutputStream os = FileSystemStorage.getInstance().openOutputStream(fileName);
OutputStream os = Storage.getInstance().createOutputStream(fileName);
ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
os.close();
EDIT: heres an example with your code mixed with the lines i provided, it works fine on the simulator for me.
String filePath = Capture.capturePhoto();
if (filePath != null) {
try {
Image img = Image.createImage(filePath);
OutputStream os = FileSystemStorage.getInstance().openOutputStream(FileSystemStorage.getInstance().getAppHomePath() + "xyz.jpg");
ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
os.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

Using Database as Alfresco ContentStore

I'm working with Alfresco 4.2 and I need to use a table in my database as document content store.
Collecting some information hither and thither over the internet, I read that I have to just implement my custom DBContentStore DBContentWriter and DBContentReader classes. Someone told me to take as reference the FileContentStore class.
I need some help to mapping the FileContentStore in order to match my new class.
For example;
The DBContentWriter has to extend AbstractContentWriter and in the API docs I read that the only methods I have to overwrite are:
getReader() to create a reader to the underlying content
getDirectWritableChannel() to write content to the repository.
What about the second method?
protected WritableByteChannel getDirectWritableChannel()
This is called by getContentOutputStream():
public OutputStream getContentOutputStream() throws ContentIOException
{
try
{
WritableByteChannel channel = getWritableChannel();
OutputStream is = new BufferedOutputStream(Channels.newOutputStream(channel));
// done
return is;
}
The main method is the putContent(InputStream is) which wants to write content into a DB table.
public final void putContent(InputStream is) throws ContentIOException
{
try
{
OutputStream os = getContentOutputStream();
copyStreams(is, os);
Where copyStreams does something like this:
public final int copyStreams(InputStream in, OutputStream out, long sizeLimit) throws IOException
{
int byteCount = 0;
IOException error = null;
long totalBytesRead = 0;
try
{
byte[] buffer = new byte[BYTE_BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1)
{
// We are able to abort the copy immediately upon limit violation.
totalBytesRead += bytesRead;
if (sizeLimit > 0 && totalBytesRead > sizeLimit)
{
StringBuilder msg = new StringBuilder();
msg.append("Content size violation, limit = ")
.append(sizeLimit);
throw new ContentLimitViolationException(msg.toString());
}
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
out.flush();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
error = e;
logger.error("Failed to close output stream: " + this, e);
}
try
{
out.close();
}
catch (IOException e)
{
error = e;
logger.error("Failed to close output stream: " + this, e);
}
}
if (error != null)
{
throw error;
}
return byteCount;
}
}
The main target is to write some code in order to write and read from the DB using these methods.
When the out.flush() is called i should have to write into the BLOB field.
thanks
Without looking at the example implementation in FileContentStore it is difficult to determine everything that getDirectWritableChennel() needs to do. Needless to say actually creating a WritableByteChannel to your database should be relatively easy.
Assuming you are using the BLOB type and you are using JDBC to get at your database then you just need to set a stream for your BLOB and turn it in to a channel.
OutputStream stream = myBlob.setBinaryStream(1);
WritableByteChannel channel = Channels.newChannel(stream);
Will you need to overwrite other methods? Maybe. If you have specific issues with those feel free to raise them.

How to achieve mp3 stream using java Servlet

Goal: build a servlet so that when I type http://xxx.com/servpage?a.mp3 in browser, I can instantaneously start the playing of this mp3 file. Previously if I put the file on goDaddy as a static file, I can do that. My software can play it right away.
Using Servlet, I can ignore what is after ?, just want this page to return the mp3 dynamically (because in the future I may return any other files). What I got is a long wait (>20 seconds), and then got the player to play it.
I followed some examples, and noticed "attachment" in the example. However, if I remove it, the mp3 won't got played even. I am usign Google App Engine though, but just use the input/outputstream to return the http request. Anyone can help?
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException ,IOException {
res.setContentType("audio/mpeg3");
OutputStream os = res.getOutputStream();
res.setHeader("Content-Disposition", "attachment; filename="" + "a.mp3";");
res.setContentLength(1000000);
FileService fileService = FileServiceFactory.getFileService();
boolean lockForRead = false;
String filename = "/gs/" + BUCKETNAME + "/" + FILENAME;
AppEngineFile readableFile = new AppEngineFile(filename);
try{
FileReadChannel readChannel = fileService.openReadChannel(readableFile, lockForRead);
InputStream is = Channels.newInputStream(readChannel);
int BUFF_SIZE = 1024;
byte[] buffer = new byte[BUFF_SIZE];
try {
do {
int byteCount = is.read(buffer);
if (byteCount == -1)
break;
os.write(buffer, 0, byteCount);
os.flush();
} while (true);
} catch (Exception excp) {
} finally {
os.close();
is.close();
}
readChannel.close();
} catch(Exception e){
}
}
Few notes:
You are not doing "streaming". Just a plain file download.
To do blob (file) serving, you do not need to read the blob from BlobStore as you do with AppEngineFile. Just serve it directly with blobstoreService.serve(blobKey). See Serving a Blob for an example.
You can get the BlobKey needed in 2. via fileService.getBlobKey(readableFile).
Update:
Just realized you are using Google Cloud Storage, not BlobStore.
In GS, if ACLs are properly set, files are publicly visible via: http://commondatastorage.googleapis.com/BUCKETNAME/FILENAME
Since you are not doing any authentication, you could publicly share the file on GS and then in your servlet just do a 301 redirect to public URL of the file.

Resources