Azure: Output not as expected in Blob Write - azure

I am trying to write output to a text file stored in my azure container.
Following is my woker role snippet for it :
string copyTemp="";
copyTemp += "hi" + "\n";
copyTemp += "hello" + "\n";
if (String.IsNullOrEmpty(copyTemp))
return;
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(System.Text.Encoding.UTF8.GetBytes(copyTemp), 0, System.Text.Encoding.UTF8.GetBytes(copyTemp).Length);
memoryStream.Position = 0;
blockBlob.UploadFromStream(memoryStream);
}
Now, when i download and check my blob,the output doesn't feature a new line.
"hihello"
Does anyone have an idea,what's goin wrong ?

Have you tried using Environment.NewLine rather than adding "\n" to your strings?
It might just be that your "\n" is not a full new line in the place you are reading it. In windows I believe you need a "\r\n" to get a proper new line character.
You can read about the differences between new line (\n) and carriage return (\r) and which systems use which on Wikipedia but that clarifies h that windows uses a carriage return and a line feed to signify a new line.
So if you had downloaded your blob on a Mac or on Linux it probably would have displayed as you expected.

The best solution for C# is to use the static string property Environment.NewLine. You can use it for writing text content to files, to azure blobs, to console output, and you will never have to think whether it is \n or \r\n. In your case the code will be:
string copyTemp="";
copyTemp += "hi" + Environment.NewLine;
copyTemp += "hello" + Environment.NewLine;

Related

Groovy - String created from UTF8 bytes has wrong characters

The problem came up when getting the result of a web service returning json with Greek characters in it. Actually it is the city of Mykonos. The challenge is whatever encoding or conversion I'm using it is always displayed as:ΜΎΚΟxCE?ΟΣ . But it should show: ΜΎΚΟΝΟΣ
With Powershell I was able to verify, that the web service is returning the correct characters.
I narrowed the problem down when the byte array gets converted to a String in Groovy. Below is code that reproduces the issue I have. myUTF8String holds the byte array I get from URLConnection.content.text. The UTF8 byte sequence to look at is 0xce, 0x9d. After converting this to a string and back to a byte array the byte sequence for that character is 0xce, 0x3f. The result of below code will show the difference at position 9 of the original byte array and the one from the converted string. For the below test I'm using Groovy Console 4.0.6.
Any hints on this one?
import java.nio.charset.StandardCharsets;
def myUTF8String = "ce9cce8ece9ace9fce9dce9fcea3"
def bytes = myUTF8String.decodeHex();
content = new String(bytes).getBytes()
for ( i = 0; i < content.length; i++ ) {
if ( bytes[i] != content[i] ) {
println "Different... at pos " + i
hex = Long.toUnsignedString( bytes[i], 16).toUpperCase()
print hex.substring(hex.length()-2,hex.length()) + " != "
hex = Long.toUnsignedString( content[i], 16).toUpperCase()
println hex.substring(hex.length()-2,hex.length())
}
}
Thanks a lot
Andreas
you have to specify charset name when building String from bytes otherwise default java charset will be used - and it's not necessary urf-8.
Charset.defaultCharset() - Returns the default charset of this Java virtual machine.
The same problem with String.getBytes() - use charset parameter to get correct byte sequence.
Just change the following line in your code and issue will disappear:
content = new String(bytes, "UTF-8").getBytes("UTF-8")
as an option you can set default charset for the whole JVM instance with the following command line parameter:
java -Dfile.encoding=UTF-8 <your application>
but be careful because it will affect whole JVM instance!
https://docs.oracle.com/en/java/javase/19/intl/supported-encodings.html#GUID-DC83E43D-52F6-41D9-8F16-318F3F39D54F

Trouble in decoding the Path of the Blob file in Azure Search

I have set a Azure search for blob storage and since the path of the file is a key property it is encoded to Base 64 format.
While searching the Index, I need to decode the path and display it in the front end. But when I try to do that in few of the scenarios it throws error.
int mod4 = base64EncodedData.Length % 4;
if (mod4 > 0)
{
base64EncodedData += new string('=', 4 - mod4);
}
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.ASCII.GetString(base64EncodedBytes);
Please let me know what is the correct way to do it.
Thanks.
Refer to Base64Encode and Base64Decode mapping functions - the encoding details are documented there.
In particular, if you're using .NET, you should use HttpServerUtility.UrlTokenDecode method with UTF-8 encoding, not ASCII.

SoapUI Load test groovy sequentially reading txt file

I am using free version of soapui. In my load test, I want to read request field value from a text file. The file looks like following
0401108937
0401109140
0401109505
0401110330
0401111204
0401111468
0401111589
0401111729
0401111768
In load test, for each request I want to read this file sequentially. I am using the code mentioned in Unique property per SoapUI request using groovy to read the file. How can I use the values from the file in a sequential manner?
I have following test setup script to read the file
def projectDir = context.expand('${projectDir}') + File.separator
def dataFile = "usernames.txt"
try
{
File file = new File(projectDir + dataFile)
context.data = file.readLines()
context.dataCount = context.data.size
log.info " data count" + context.dataCount
context.index = 0; //index to read data array in sequence
}
catch (Exception e)
{
testRunner.fail("Failed to load " + dataFile + " from project directory.")
return
}
In my test, I have following script as test step. I want to read the current index record from array and then increment the index value
def randUserAccount = context.data.get(context.index);
context.setProperty("randUserAccount", randUserAccount)
context.index = ((int)context.index) + 1;
But with this script, I always get 2nd record of the array. The index value is not incrementing.
You defined the variable context.index to 0 and just do +1
You maybe need a loop to read all values.
something like this :
for(int i=0; i <context.data.size; i++){
context.setProperty("randUserAccount", i);
//your code
}
You can add this setup script to the setup script section for load test and access the values in the groovy script test step using:
context.LoadTestContext.index =((int)context.LoadTestContext.index)+1
This might be a late reply but I was facing the same problem for my load testing for some time. Using index as global property solved the issue for me.
Index is set as -1 initially. The below code would increment the index by 1, set the incremented value as global property and then pick the context data for that index.
<confirmationNumber>${=com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "index", (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" ).toLong()+1 ).toString()); return (context.data.get( (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" )).toInteger())) }</confirmationNumber>

Zip multiple files using 7zip.exe in C#

I have to zip multiple files together using 7zip.exe. I have paths of two files say file1 and file2. I append the two paths using the following.
string filetozip = file1+ "\"" + file2+" "; and do the below
Process proc = new Process();
proc.StartInfo.FileName = #"C:\Freedom\7-Zip\7z.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.Arguments = string.Format(" a -tzip \"{0}\" \"{1}\" -mx=9 -mem=AES256 -p\"{2}\" ", destZipFile, filetozip , zipPassword);
proc.Start();
proc.WaitForExit();
if (proc.ExitCode != 0)
{
throw new Exception("Error Zipping Data File : " + proc.StandardError.ReadToEnd());
}
filetozip is passed as an argument above. The above code does not work properly. I am getting proc.ExitCode=1. Which is the right way to append the file paths.Is string filetozip = file1+ "\"" + file2+" "; the right way? I can have one or more files. What is the separator used?
The command line that you want to create looks like
plus the required switches (arguments quoted and space delimited).
String.Join or StringBuilder are some coding things that may be helpful

Why is File.separator using the wrong character?

I'm trying to add functionality to a large piece of code and am having a strange problem with File Separators. When reading a file in the following code works on my PC, but fails when on a Linux server. When on PC I pass this and it works:
fileName = "C:\\Test\\Test.txt";
But when on a server I pass this and get "File Not Found" because the BufferedReader/FileReader statement below swaps "/" for "\":
fileName = "/opt/Test/Test.txt";
System.out.println("fileName: "+fileName);
reader = new BufferedReader(new FileReader(new File(fileName)));
Produces this output when run on the LINUX server:
fileName: /opt/Test/Test.txt
File Not Found: java.io.FileNotFoundException: \opt\Test\Test.txt (The system cannot find the path specified)
When I create a simple Test.java file to try and replicate it behaves as expected, so something in the larger code source is causing the BufferedReader/FileReader line to behave as if it's on a PC, not a Linux box. Any ideas what that could be?
I don't see where you used File.separator. Try this instead of hard coding the path separators.
fileName = File.separator + "opt" + File.separator + "Test" + File.separator + "Test.txt";

Resources