Convert string to bytes but getting error java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I tried to do "image = (PNMImage) ois.read(getBytes(fileName);" in order to fix the issue of converting string to byte but I got an error that said, cannot resolve getBytes in PNMimage
// TODO Implement this method
// return null;
PNMImage image = null;
try {
DataInputStream ois = new DataInputStream(new FileInputStream(fileName));
image = (PNMImage) ois.read(getBytes(fileName));
}catch(ClassNotFoundException e ) {
e.printStackTrace();
}
return image;
}```

Related

How to validate URL pattern of ER.RTR.RT12345? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am facing issue for validate the URL pattern like ER.RTR.RT12345,its return me true .But it is works fine for https://www.sophos.com/cs-cz/support/knowledgebase/117316.aspx this.
public static boolean validateURL(String url) {
String urlPattern = "(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?(https://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?";
if (url.matches(urlPattern))
return true;
else
return false;
}
How to resolves this issue ?
Java's URL class automatically "validates" a URL string. The validation is according to
The syntax of URL is defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs.
You can just use the constructor:
public static void main(String[] args) {
try {
URL url = new URL("ER.RTR.RT12345");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
and manage true or false with the catch block. The above example throws the exception.

How to use URLConnection.getContentLength() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have this rather weird problem with my program. What I try to do is to check the Content-Length of a URL and then use this number in a non-static method with an if-else statement connecting other classes. But my problem with this is that while I was researching the internet to somehow solve this problem, there where these static-methods that I could use to get the contentLength and it wouldn't work with non-static. So I tried it but I never really got any results. What I basically tried to do is to call the class with the static-method via non-static-method in another class which, I guess, shouldn't be the problem (or should? I don't really know since I have never worked with static methods before). Then I used this code to determine the contentLength of a random webpage with changing date:
public static void main() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 1); // Adding 1 day
String output = sdf.format(c.getTime());
try {
String urlStr = "http://www.sasintern.de/show_pdf.php?fn=" + output + ".pdf";
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int contentLength = httpConn.getContentLength();
if (contentLength < 2000) {
new NotWorkingActivity();
System.out.println("unknown content length");
} else {
new download2Activity();
System.out.println("content length: " + contentLength + " bytes");
}
InputStream inStream = httpConn.getInputStream();
// now read data
// ...
// close connection
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Afterwards, when the size of the page was determined to be smaller than 2000 bytes, it should show a Toast in another class that the page is not available, but when it was bigger, then it should open up a new class, as you may see in the else-statement.
But it doesn't work and I think I just messed up with static methods. I'm just not that experienced with them. Sorry for any other mistakes that I might have made. If there is an different solution to my problem which, hopefully, is easier to understand and better to use, I would be very grateful.
Please, I need help on this problem and I hope you understand what I am up to.
Thanks in advance :)
Read the manual
http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getContentLength()
public int getContentLength()
Returns the value of the content-length header field.
Not all resources set the Content-Length header. So, you need to obtain input stream and read all the bytes until the end to count them.

FileInputStream / ObjectInputStream: StreamCorruptedException: Wrong format [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When reading a serialized object from a file, I get:
java.io.StreamCorruptedException: Wrong format: 0
The object, which implements Serializable is saved and restored as such:
Save:
try {
FileOutputStream fileOutputStream = getContext().openFileOutput("gameState.ser", Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(gameAssets);
} catch (Exception e) {
e.printStackTrace();
}
Restore:
try {
FileInputStream fileInputStream = getContext().openFileInput("gameState.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
gameAssets = (GameAssets) objectInputStream.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Since you haven't posted the surrounding code, I am not sure if this is the case, or the source of the error.
But you should always close your streams after writing to them.
Make sure all the fields on gameAssets are serializeable. If one of them is not an exception can be printed into the file you created which can cause an exception on read of that file.

Running Javascript Code Used to Run on NodeJS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code that is running on NodeJs. We would like to change the technology (to java).
The problem is we have some existing passwords, and I am not sure how to I copy the encryption logic to java.
So, one of the possible solutions is to run the encryption logic in javascript (e.g. command line, embbeded in the java, etc) and get the result back.
The question is - how do I do that?
The nodejs code goes like this:
crypto = require('crypto');
this.salt = this.makeSalt();
encryptPassword: function(password) {
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, iterations, keylen).toString('base64');
crypto.randomBytes(..)
}
makeSalt: function() {
return crypto.randomBytes(numOfBytes).toString('base64');
},
UPDATE:
Following the suggestions here, I added the full code. If the right way of doing it is by transforming the javascript code to java code, can you please help me translated the above code?
You should not do this, if you want random bytes in Java do this. You should be able to replicate the encryption logic in Java.
byte[] b = new byte[20];
new Random().nextBytes(b);
Almost all of the Node.js crypto functions are generic, and should have their own Java counterparts or 3rd party libraries.
Update
If you must run your node code via java you can add this method
public static String runCommand(String command) {
String output = "";
try {
String line;
Process process = Runtime.getRuntime().exec( command );
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()) );
while ((line = reader.readLine()) != null) {
output += line;
}
reader.close();
} catch (Exception exception) {
// ...
}
return output;
}
and run it like this
String encryptedPassword = runCommand("node myEncryption.js --password=1234");

How to retrieve specific information from a certain website? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am developing a java web application and I want to know how to take a certain field (table and/or output-text) value from a certain website. Assuming that this component has always the same ID does anyone know how can I retrieve this information?
I don't know if anyone has ever faced this issue but if anyone has any idea please share.
Thank you.
In general:
1.) Retrieve the pages markup by reading it through an HTTPConnection to the URL in your application
2.) Parse the Markup using a framework like jsoup and retrieve the value you need.
More specifically, here is some example code for jsoup:
HttpClient http = new DefaultHttpClient();
String htmlcode = "";
HttpGet request = new HttpGet("http://www.example.com");
HttpResponse response = null;
try {
response = http.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(response != null){
BufferedReader read = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while((line = read.readLine()) != null){
htmlcode += line;
}
}
// at this point we have the pages markup
Document doc = Jsoup.parse(htmlcode);
Elements lis = doc.getElementsByTag("li"); // get all entries in lists
for(Element el : lis){
String val = el.text().trim();
// do something for each list entry
}
You are talking about web scraping, check this library for php:
http://simplehtmldom.sourceforge.net/

Categories