0

Actually, I require

A valid Base64 string value that identifies the block. Prior to encoding, the string must be less than or equal to 64 bytes in size.

For a given blob, the length of the value specified for the blockid parameter must be the same size for each block.

Note that the Base64 string must be URL-encoded.

as stated in https://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/put-block.

For that, I am converting some C# code to Java. And I am having problem with this line of code

//create a blockID from the block number, add it to the block ID list
                      //the block ID is a base64 string
String blockId = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}",blockNumber.ToString("0000000"))));

I have tried below:

String str = "BlockId"+blockNumber;
String blockId = Base64.getEncoder().encodeToString(("BlockId"+(blockNumber+"")).getBytes("utf-8"));

But, I am not able to understand what "0000000" as a argument to ToString doing and what is its equivalent in Java.

And also how can I fulfil the second condition that length of blockid of each block or chance file should be same in JAVA. As file size can vary.

juunas
  • 54,244
  • 13
  • 113
  • 149
Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36

3 Answers3

2

In C#, toString() has several overloaded versions. The one that accepts a String parameter uses the String to specify the format. In this case, the number will be padded with enough 0s to ensure there are 7 digits. In Java, you can use String.format() to get the same behavior. See the format string documentation for details about format string syntax.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

In your C# example code blockNumber.ToString("0000000") just pads blockNumber with leading zeros to a fixed-length string of size 7. You can achieve the same in Java with String.format().

To encode this string with the correct Base64 scheme that can be used in URLs, you can use Base64.getUrlEncoder() since Java 8.

A complete Java 8 example:

long blockNumber = 123;

// left pad block number with zeros to a fixed length of 7
String fixedLengthBlockNumber = String.format("%07d", blockNumber);

// Base64 encode using the "URL and Filename safe Base64 Alphabet"
String blockId = Base64.getUrlEncoder().encodeToString(fixedLengthBlockNumber.getBytes("utf-8"));

The blockNumber is just a number you assign to your blocks. It may or may not be derived from the block content itself. If it's derived from the block content (e.g. via MD5 checksum), the checksum itself already fulfils the fixed length criterion and you even don't have to pad the number.

René Scheibe
  • 1,970
  • 2
  • 14
  • 20
  • But, the problem with above I am having is that if you give number greater then 7 digit ex "99888877" it will print as it is with error. – Arjun Chaudhary Jan 29 '17 at 18:00
  • `String.format()` doesn't print anything, it just returns a string. If your number has more digits than the padding length in the format pattern then the number just isn't padded. – René Scheibe Jan 29 '17 at 21:14
0

The "00000000" parameter tells the Format method to create an 8-digit string with leading 0s. See this MSDN documentation

For the issue with fixed block size, you could fill up with = characters. When you read your data, remove all = characters and then fill up to a length that is multiple-of-3. See here

Community
  • 1
  • 1
Christoph Bimminger
  • 1,006
  • 7
  • 25