How can I convert base64 binary data into wkb - well known binary? - java

I am stuck with conversion of base 64 binary converted using shptosql into sql server. now I want to convert the data into postgres, which supports wkb- well known binary. How can I convert base64 bytes into suppoorted format which is well known binary in java? I have decoded base64 but wkb is still far from my point.
Part 1 : Read XML Part (Done)
Part 2 : Extract Binary data Part (Done)
Part 3 : Convert into wkb (Left)
Part 4 : Store wkb in postgresSQL
File f = new File(request.getRealPath("/")+"themes\\ProfileImages\\temp.xml");
ArrayList<byte[]> byteArray = new ArrayList<byte[]>();
if(f.exists()){
List<String> list = geoserver.getShapes(f);
for(String s : list){
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes=null;
try {
decodedBytes = decoder.decodeBuffer(s);
byteArray.add(decodedBytes);
//Here I want to convert bytes to wkb format and want to save in postgres
} catch (IOException e) {
e.printStackTrace();
}
}
List<String> objects =geoserver.getObjectIds(f);
List<ShapeFile> shapes = new ArrayList<ShapeFile>();
for(int i=0;i<byteArray.size();i++){
try {
shapes.add(new ShapeFile(objects.get(i), byteArray.get(i),IOUtils.toString(byteArray.get(i))));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Part 3 is left for me.
Can someone please tell me how and using which libraries I can do that?

Related

How to convert an Image to base64 string in java? [duplicate]

This question already has answers here:
Encoding as Base64 in Java
(19 answers)
Closed 6 years ago.
It may be a duplicate but i am facing some problem to convert the image into Base64 for sending it for Http Post. I have tried this code but it gave me wrong encoded string.
public static void main(String[] args) {
File f = new File("C:/Users/SETU BASAK/Desktop/a.jpg");
String encodstring = encodeFileToBase64Binary(f);
System.out.println(encodstring);
}
private static String encodeFileToBase64Binary(File file){
String encodedfile = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
encodedfile = Base64.encodeBase64(bytes).toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedfile;
}
Output: [B#677327b6
But i converted this same image into Base64 in many online encoders and they all gave the correct big Base64 string.
Edit: How is it a duplicate?? The link which is duplicate of mine doesn't give me solution of converting the string what i wanted.
What am i missing here??
The problem is that you are returning the toString() of the call to Base64.encodeBase64(bytes) which returns a byte array. So what you get in the end is the default string representation of a byte array, which corresponds to the output you get.
Instead, you should do:
encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8");
I think you might want:
String encodedFile = Base64.getEncoder().encodeToString(bytes);
this did it for me. you can vary the options for the output format to Base64.Default whatsoever.
// encode base64 from image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
encodedString = Base64.encodeToString(b, Base64.URL_SAFE | Base64.NO_WRAP);

Java Decompressing byte array - incorrect data check

I have a little problem: I decompress byte array and everything is ok with following code but sometimes with some data it throws DataFormatException with incorrect data check. Any ideas?
private byte[] decompress(byte[] compressed) throws DecoderException {
Inflater decompressor = new Inflater();
decompressor.setInput(compressed);
ByteArrayOutputStream outPutStream = new ByteArrayOutputStream(compressed.length);
byte temp [] = new byte[8196];
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(temp);
logger.info("count = " + count);
outPutStream.write(temp, 0, count);
}
catch (DataFormatException e) {
logger.info(e.getMessage());
throw new DecoderException("Wrong format", e);
}
}
try {
outPutStream.close();
} catch (IOException e) {
throw new DecoderException("Cant close outPutStream ", e);
}
return outPutStream.toByteArray();
}
Try with a different compression level or using the nowrap options
1 Some warning: do you use the same algorithm in both sides ?
do you use bytes ? (not String)
your arrays have the good sizes ?
2
I suggest you check step by step, catching exceptions, checking sizes, null, and comparing bytes.
like this: Using Java Deflater/Inflater with custom dictionary causes IllegalArgumentException
Take your input
Compress it
copy your bytes
decompress them
compare output with input
3 if you cant find, take another example which works, and modify it step by step
hope it helps
I found out why its happening
byte temp [] = new byte[8196];
its too big, it must be exactly size of decompressed array cause it was earlier Base64 encoded, how i can get this size before decompressing it?

Consume a c# base64 encoded file in java

I want to transfer a file from C# to a java webservice which accepts base64 strings. The problem is that when I encode the file using the c# Convert class, it produces a string based on a little endian unsigned byte[].
In Java byte[] are signed / big endian. When I decode the delivered string, I get a different byte[] and therefor the file is corrupt.
How can I encode a byte[] in C# to a base64, which is equal to the byte[] that is decoded in java using the same string?
C# side:
byte[] attachment = File.ReadAllBytes(#"c:\temp\test.pdf");
String attachmentBase64 = Convert.ToBase64String(attachment, Base64FormattingOptions.None);
Java side:
#POST
#Path("/localpdf")
#Consumes("text/xml")
#Produces("text/xml")
public String getExtractedDataFromEncodedPdf(#FormParam("file") String base64String) {
if(base64String == null) return null;
byte[] data = Base64.decodeBase64(base64String.getBytes());
FileOutputStream ms;
try {
ms = new FileOutputStream(new File("C:\\Temp\\test1234.pdf"));
ms.write(data);
ms.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File test1234.pdf is corrupt.
"Signed" and "big-endian" are very different things, and I believe you're confusing yourself.
Yes, bytes are signed in Java and unsigned in C# - but I strongly suspect you're getting the same actual bits in both cases... it's just that a bit pattern of (say) 11111111 represents 255 in C# and -1 in Java. Unless you're viewing the bytes as numbers (which is rarely useful) it won't matter - it certainly doesn't matter if you just use the bytes to write out a file on the Java side.

how to convert text file of base64 into xml file(or any other type of file)

I have these Base64 in a text file:
77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxSb290Pg0KICA8SXRl
bXM+DQogICAgPEl0ZW0gTmFtZT0iQ2xhaW1OdW1iZXIiIFZhbHVlPSI0ODY1MTQ4MDQiIC8+DQog
ICAgPEl0ZW0gTmFtZT0iRG9jVHlwZSIgVmFsdWU9IkxpdGlnYXRpb24iIC8+DQogICAgPEl0ZW0g
TmFtZT0iRG9jU3ViVHlwZSIgVmFsdWU9IklOVCBBbnN3ZXJzIiAvPg0KICAgIDxJd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PE5vdGUgLz4NCiAgPC9Sb3V0ZT4NCjwvUm9vdD4=
I need to be able to take those base 64 charecters from the text file and output a new XML File. Currently, the InputStream is not being correctly converted to base 64
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\khurt\\Desktop\\xml.txt");
InputStream myScan = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
myScan.read(b);
String cowo = myScan.toString();
String decoded = DatatypeConverter.printBase64Binary(b);
String cat = b.toString();
System.out.println(decoded);
byte[] bArray = cat.getBytes();
OutputStream out = new FileOutputStream("C:\\Users\\gdfurt\\Desktop\\cow.xml");
out.write(b);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I used the System.out.println(decode); to check to see if the charecters matched the ones in the file and they do not. I can't figure out why, I have tried using a scanner and that throws it off more.
Data you have got is Base64 encoded. and you are not decoding it in your code. That is main reason behind other programs cannot read it as XML file.
Another is hidden in your Bytes of data. Start of Byte data is 77u/ which is saying data is BINARY data and becomes problem here.
Use Link to experience decoded data:
http://www.opinionatedgeek.com/dotnet/tools/base64decode/
If you will use 77u/ at start of data you will experience data is BINARY and will get downloaded as file. And if you do not use 77u/ it will show output online only.
Remove first 4 char while processing your data and then you are good to go inside java code only.
EDIT
Please use below code snippet. You are re-encoding byte array. You need to decode it. Also this process needs little bit conversions of String to Byte and vice-versa.
try {
File file = new File("C:\\Users\\ABC\\Desktop\\xml.txt");
InputStream myScan = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
myScan.read(b);
String cowo = new String(b);
System.out.println( cowo );
String decoded = new String(DatatypeConverter.parseBase64Binary(cowo));
String cat = b.toString();
System.out.println(decoded);
byte[] bArray = cat.getBytes();
OutputStream out = new FileOutputStream("C:\\Users\\ABC\\Desktop\\cow.xml");
out.write(decoded.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read the bytes:
byte[] b;
try (InputStream in = new FileInputStream(file)) {
b = new byte[(int) file.length()];
in.read(b);
} // Closes in
Which in Java 7 goes easier:
b = Files.readAllBytes(file.toPath());
or immediately with Path i.o. Fiile:
Path path = Paths.get("C:\\Users\\khurt\\Desktop\\xml.txt");
b = Files.readAllBytes(path);
As Base64 only uses ASCII do:
String encoded = new String(b, StandardCharsets.US_ASCII);
Parse Base64 text to byte[]
b = DatatypeConverter.parseBase64Binary(encoded);
If you want the XML as text:
String decoded = new String(b, StandardCharsets.UTF_8);
By the way, the XML starts with "\ufeff" the Unicode BOM character, which is redundand.
Addendum 2021-11-16
Nowadays there is one Base64 class in java SE:
b = Base64.getDecoder().decode(b);
or even (suitable for large files):
b = Base64.getDecoder().decode(Files.newInputStream(path));

How to convert a byte array into an AudioInputStream in Java

I want to convert a byte array into an AudioInputStream. The byte array was filled from a *.wav file before. I have the following code:
public static AudioInputStream writeBytesBackToStream(byte[] bytes) {
ByteArrayInputStream baiut = new ByteArrayInputStream(bytes);
AudioInputStream stream = null;
try {
stream = AudioSystem.getAudioInputStream(baiut);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(stream.equals(null) || stream == null) {
System.out.println("WARNING: Stream read by byte array is null!");
}
return stream;
}
Now I only want to convert this byte array into an AudioInputStream, but an UnsupportedAudioFileException is thrown:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
Has anyone got an idea?
Another way to do this, if the data is actually PCM, is to set the AudioFormat parameters manually. Here is an example to create an A minor.
byte[] b = new byte[64000];
//lets make a 440hz tone for 1s at 32kbps, and 523.25hz.
for(int i = 0; i<b.length/2; i++){
b[i*2+1] = (byte)(127*Math.sin(4*Math.PI*440.0/b.length*i));
b[i*2] = (byte)(127*Math.sin(4*Math.PI*523.25/b.length*i));
}
AudioInputStream stream = new AudioInputStream(
new ByteArrayInputStream(b),
new AudioFormat(16000, 8, 2, true, false),
64000
);
Also be careful if you're making bytes into sounds and you have earphones in.
If you properly read the WAV file, the byte array will just contain raw PCM data. Consequently the AudioSystem can not identify the format of the stream and throws the exception.
This can not work by design. You need to provide a complete audio format image in your stream to have AudioSystem recognize what format the stream is, not just raw data.

Categories

Resources