binary encoder in camel netty - apache-camel

I converted the ebcdic input to ascii using this code using apache camel netty..
How to convert binary input to ascii? I tried with all the available charsetutil but its not working..
Any suggestions or answers available...
import java.beans.Encoder;
import org.apache.camel.main.Main;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.CharsetUtil;
/**
* Starting point for application
* #author SubramaniMohanam
*/
public class MainApp {
/**
* Main method
* Encoders and decoders are added here
* Route builders are added
*/
#SuppressWarnings("deprecation")
public static void main(String... args) throws Exception {
Main main = new Main();
main.enableHangupSupport();
System.out.println("main started ...");
main.bind("decoder", new LengthFieldBasedFrameDecoder(40, 0, 1,0,0));
main.bind("decoder", new LengthFieldBasedFrameDecoder(1024, 4, 2,0,17));
main.bind( "stringEncoder", new StringEncoder("Cp1047"));
main.bind("stringDecoder", new StringDecoder("Cp1047"));
main.addRouteBuilder(new StationRouteBuilder());
main.run(args);
}
}

I guess that your question is, how to decode EBCDIC binary data to ASCII data? If this is the case, have a look at Converting EBCDIC to ASCII in java and write your own Camel decoder. More information about encoders/decoders can be found here: Can someone better explain Decoders/Encoders?
That said, all encoders/decoders should have a unique binding name (you used "decoder" twice).

Related

Apache Flink - Nothing printed to the output, even if the exit code is 0

I am using Apache Flink 1.16.0 version.
I am trying to do a simple CEP by printing the elements to the console
For any reason, there is nothing printed to the console, even the process finished with exit code 0.
Here is the code:
import org.apache.flink.cep.*;
import org.apache.flink.cep.functions.PatternProcessFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> input = env.fromElements("adrian", "cincu", "diana", "sichitiu");
Pattern<String, ?> pattern = Pattern.begin("start");
PatternStream<String> patternStream = CEP.pattern(input, pattern);
DataStream<String> matches = patternStream.process(new PatternProcessFunction<String, String>() {
#Override
public void processMatch(Map<String, List<String>> map,
Context context,
Collector<String> collector) throws Exception {
collector.collect(map.get("start").toString());
}
});
matches.print();
env.execute("myjob");
}
}
Any clue?
I think the issue in this case is that Your pattern doesn't make sense. It will just start matching events, but there is no actual requirement for events to match the pattern, so every event (String in Your case) will match the same Pattern, this will cause the Pattern to stay open forever and never emit any actual data.
You may want to add some kind of requirements to make sure that pattern actually gets closed.
There is some info about how to define Patterns in the [docs].1

Codename One "out of memory" when using Object-C native interface (HEIC to JPEG conversion)

Since I'm implementing a custom gallery for Android and iOS, I have to access directly to the gallery files stored in the FileSystemStorage through native interfaces.
The basic idea is to retrieve the file list through a native interface, and then make a cross-platform GUI in Codename One. This works on Android, I had to make the thumbs generation (in the Codename One side, not in the native interface side) as fast as possible and the overall result is quite acceptable.
On iOS, I have an additional issue, that is the HEIC image file format, that needs to be converted in JPEG to become usable in Codename One. Basically, I get the file list through the code in this question (I'm waiting for an answer...), then I have to convert each HEIC file to a temporary JPEG file, but my HEICtoJPEG native interface makes the app crashing after few images with an "out of memory" Xcode message...
I suspect that the problematic code is the following, maybe the UIImage* image and/or the NSData* mediaData are never released:
#import "myapp_utilities_HEICtoJPEGNativeImpl.h"
#implementation myapp_utilities_HEICtoJPEGNativeImpl
-(NSData*)heicToJpeg:(NSData*)param{
UIImage* image = [UIImage imageWithData:param];
NSData* mediaData = UIImageJPEGRepresentation(image, 0.9);
return mediaData;
}
-(BOOL)isSupported{
return YES;
}
#end
This is the Java native interface:
import com.codename1.system.NativeInterface;
/**
* #deprecated
*/
public interface HEICtoJPEGNative extends NativeInterface {
public byte[] heicToJpeg(byte[] heicInput);
}
and this the Java public API:
import com.codename1.io.FileSystemStorage;
import com.codename1.io.Util;
import com.codename1.system.NativeLookup;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class HEICtoJPEG {
private static HEICtoJPEGNative nativeInterface = NativeLookup.create(HEICtoJPEGNative.class);
/**
* Public API to convert an HEIC file to a new JPEG file (placed in /heic)
* #param heicFile in the FileSystemStorage
* #return a new file (with unique name)
*/
public static String convertToJPEG(String heicFile) throws IOException {
if (nativeInterface != null && nativeInterface.isSupported()) {
// It ensures that the directory exists.
FileSystemStorage fss = FileSystemStorage.getInstance();
String heicDir = fss.getAppHomePath() + "/heic";
if (!fss.isDirectory(heicDir)) {
fss.mkdir(heicDir);
}
ByteArrayOutputStream outHeic = new ByteArrayOutputStream();
InputStream inHeic = fss.openInputStream(heicFile);
Util.copy(inHeic, outHeic);
byte[] heicData = outHeic.toByteArray();
byte[] jpegData = nativeInterface.heicToJpeg(heicData);
String jpegFile = heicDir + "/" + DeviceUtilities.getUniqueId() + ".jpg";
OutputStream outJpeg = fss.openOutputStream(jpegFile);
ByteArrayInputStream inJpeg = new ByteArrayInputStream(jpegData);
Util.copy(inJpeg, outJpeg);
return jpegFile;
} else {
return null;
}
}
}
Since the Android counterpart works, I hope that the rest of my custom gallery code is fine and that this out-of-memory issue is inside code I posted here.
I hope you can indicate me a working solution. Thank you
There was a memory leak in the way that the iOS port invoked native interface methods which received or returned primitive arrays (byte[], int[], etc..).
I have just committed a fix for this (native interface invocations are now wrapped in an autorelease pool) which will be available on the build server next Friday (October 9, 2020).
EDIT: (Friday October 2, 2020)
This fix has been deployed to the build server already so it you should be able to build it again immediately and see if it fixes your issue.

javamail throws java.io.UnsupportedEncodingException: unknown-8bit

There were some emails that I try to read using javamail lib. When the email contains the MIME header (Content-Type: text/plain; charset="unknown-8bit"), I get this error: java.io.UnsupportedEncodingException: unknown-8bit
Any ideas why is this happening?
Because "unknown-8bit" is not a known charset name. This is explained in the JavaMail FAQ, along with alternatives for handling this problem. I've copied the answer here but note that this may become out of date. Please be sure to search the JavaMail FAQ for any other JavaMail problems you might have.
Q: Why do I get the UnsupportedEncodingException when I invoke getContent() on a bodypart that contains text data?
A: Textual bodyparts (i.e., bodyparts whose type is "text/plain", "text/html", or "text/xml") return Unicode String objects when getContent() is used. Typically, such bodyparts internally hold their textual data in some non Unicode charset. JavaMail (through the corresponding DataContentHandler) attempts to convert that data into a Unicode string. The underlying JDK's charset converters are used to do this. If the JDK does not support a particular charset, then the UnsupportedEncodingException is thrown. In this case, you can use the getInputStream() method to retrieve the content as a stream of bytes. For example:
String s;
if (part.isMimeType("text/plain")) {
try {
s = part.getContent();
} catch (UnsupportedEncodingException uex) {
InputStream is = part.getInputStream();
/*
* Read the input stream into a byte array.
* Choose a charset in some heuristic manner, use
* that charset in the java.lang.String constructor
* to convert the byte array into a String.
*/
s = convert_to_string(is);
} catch (Exception ex) {
// Handle other exceptions appropriately
}
}
There are some commonly used charsets that the JDK does not yet support. You can find support for some of these additional charsets in the JCharset package at http://www.freeutils.net/source/jcharset/.
You can also add an alias for an existing charset already supported by the JDK so that it will be known by an additional name. You can create a charset provider for the "bad" charset name that simply redirects to an existing charset provider; see the following code. Create an appropriate CharsetProvider subclass and include it along with the META-INF/services file and the JDK will find it. Obviously you could get significantly more clever and redirect all unknown charsets to "us-ascii", for instance.
==> UnknownCharsetProvider.java <==
import java.nio.charset.*;
import java.nio.charset.spi.*;
import java.util.*;
public class UnknownCharsetProvider extends CharsetProvider {
private static final String badCharset = "x-unknown";
private static final String goodCharset = "iso-8859-1";
public Charset charsetForName(String charset) {
if (charset.equalsIgnoreCase(badCharset))
return Charset.forName(goodCharset);
return null;
}
public Iterator<Charset> charsets() {
return Collections.emptyIterator();
}
}
==> META-INF/services/java.nio.charset.spi.CharsetProvider <==
UnknownCharsetProvider

Download the file using Camel ConsumerTemplate multiple times

I am trying to download the same file multiple times using Camel ConsumerTemplate. It's not getting downloaded multiple times, but I am getting it done while using camel route.
I want to download the file using ConsumerTemplate multiple times. Here is the code that I am trying with ConsumerTemplate:
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class DynamicConsumer implements Processor {
#Override
public void process(Exchange inExchange) throws Exception {
CamelContext camelContext = inExchange.getContext();
ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
String resource = "sftp://tester#localhost:22/myfiles?password=password&noop=true&idempotent=false&readLockMarkerFile=false&readLock=none&filter=#myFileFilter";
consumerTemplate.start();
Exchange resourceExchange = consumerTemplate.receive(resource,20000);
consumerTemplate.stop();
if(resourceExchange != null) {
inExchange.getOut().setBody(resourceExchange.getIn().getBody());
inExchange.getProperties().putAll(resourceExchange.getProperties());
inExchange.getOut().getHeaders().putAll(resourceExchange.getIn().getHeaders());
} else {
inExchange.getOut().setBody(null);
}
}
}
This dynamic consumer is getting called from the camel routes multiple times. So, each time, I expect to download the file present in given location. But it is not happening.
Here the code that I tried with camel route.
<from uri="sftp://tester#localhost:22/myfiles?password=password&noop=true&idempotent=false&readLockMarkerFile=false&readLock=none&filter=#myFileFilter"/>
As the documentation states you should call the ConsumerTemplate's –doneUoW(Exchange) function:
If you have used any of the receive methods which returns a Exchange
type then you need to invoke this method when you are done using the
returned Exchange.
-- ConsumerTemplate documentation
Try to add the following before consumerTemplate.stop():
consumerTemplate.doneUoW(resourceExchange);

JavaFX - Playing loop video

How should I loop a video in JavaFX?
I'm trying to just play a video one time after another, so I was looking for some sample code in many places and I could'nt make it work!
This is what doesn't work for me:
public MyMediaPlayer (){
media = new Media(getVideo());
mediaPlayer = new MediaPlayer(media);
mediaView = new MediaView(mediaPlayer);
startMediaPlayer();
}
private String getVideo() {
return getClass().getResource("videos/limbo.mp4").toString();
}
public final void startMediaPlayer() {
mediaPlayer.setMute(true);
mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE); //this is the line that should do the magic, but it doesn't...
mediaPlayer.play();
}
The following works for me (video loops forever). I can't replicate your issue.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;
public class VideoPlayerExample extends Application {
public static void main(String[] args) throws Exception { launch(args); }
#Override public void start(final Stage stage) throws Exception {
final MediaPlayer oracleVid = new MediaPlayer(
new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv")
);
stage.setScene(new Scene(new Group(new MediaView(oracleVid)), 540, 208));
stage.show();
oracleVid.setMute(true);
oracleVid.setRate(20);
oracleVid.setCycleCount(MediaPlayer.INDEFINITE);
oracleVid.play();
}
}
I'm under Java 7, doesn't work there . . . the problem seems to be MP4 format.
If you can't play MP4 files, either:
The MP4 is not encoded in a format JavaFX understands (the JavaFX 2.2 Media javadoc details the allowed formats).
OR
You don't have appropriate codecs installed on your machine to allow the MP4 file to be decoded. See the JavaFX 2.2 Media system requirements for information on what you need to install on your machine to allow MP4 files to be displayed.

Resources