Last active
January 31, 2018 15:43
-
-
Save webfirmframework/57c971cfdc211c50296e454f8317e387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014-2016 Web Firm Framework | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.webfirmframework.wffweb.common.algo; | |
import java.io.UnsupportedEncodingException; | |
import com.webfirmframework.wffweb.util.data.NameValue; | |
public class CustomNameValue extends NameValue { | |
private static final long serialVersionUID = 1L; | |
public CustomNameValue() { | |
} | |
public static class Builder { | |
private byte[] name; | |
private byte[][] values; | |
public Builder() { | |
} | |
public Builder name(final String name) { | |
try { | |
this.name = name.getBytes("UTF-8"); | |
} catch (final UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return this; | |
} | |
public Builder name(final byte[] name) { | |
this.name = name; | |
return this; | |
} | |
public Builder values(final String... values) { | |
this.values = new byte[values.length][0]; | |
try { | |
for (int i = 0; i < this.values.length; i++) { | |
this.values[i] = values[i].getBytes("UTF-8"); | |
} | |
} catch (final UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return this; | |
} | |
public Builder values(final byte[]... values) { | |
this.values = values; | |
return this; | |
} | |
public CustomNameValue build() { | |
final CustomNameValue customNameValue = new CustomNameValue(); | |
customNameValue.setName(name); | |
customNameValue.setValues(values); | |
return customNameValue; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014-2016 Web Firm Framework | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.webfirmframework.wffweb.common.algo; | |
import java.util.LinkedList; | |
import java.util.List; | |
import com.webfirmframework.wffweb.util.WffBinaryMessageUtil; | |
import com.webfirmframework.wffweb.util.data.NameValue; | |
public class WffBinaryMessageUsage { | |
/** | |
* This method will print as follows | |
* | |
* <pre> | |
* -------- At index 0 ---------- | |
* name usernames | |
* values user1 | |
* values user2 | |
* | |
* -------- At index 1 ---------- | |
* name profile_pics | |
* values pic1 | |
* values pic2 | |
* | |
* </pre> | |
* | |
* @param args | |
* @since 1.0.0 | |
* @author WFF | |
*/ | |
public static void main(final String[] args) { | |
final NameValue nameValueUsernames = new CustomNameValue.Builder() | |
.name("usernames").values("user1", "user2").build(); | |
// holds bytes of profile pics | |
final byte[] profilePic1 = { 'p', 'i', 'c', '1' }; | |
final byte[] profilePic2 = { 'p', 'i', 'c', '2' }; | |
final NameValue nameValueProfilePics = new CustomNameValue.Builder() | |
.name("profile_pics").values(profilePic1, profilePic2).build(); | |
final List<NameValue> nameValues = new LinkedList<NameValue>(); | |
nameValues.add(nameValueUsernames); | |
nameValues.add(nameValueProfilePics); | |
final byte[] wffBinaryMessageBytes = WffBinaryMessageUtil.VERSION_1 | |
.getWffBinaryMessageBytes(nameValues); | |
final List<NameValue> nameValuesParsed = WffBinaryMessageUtil.VERSION_1 | |
.parse(wffBinaryMessageBytes); | |
System.out.println("-------- At index 0 ----------"); | |
final NameValue usernames = nameValuesParsed.get(0); | |
System.out.println("name " + new String(usernames.getName())); | |
final byte[][] usernameValues = usernames.getValues(); | |
for (final byte[] value : usernameValues) { | |
System.out.println("values " + new String(value)); | |
} | |
System.out.println("\n-------- At index 1 ----------"); | |
final NameValue profilePics = nameValuesParsed.get(1); | |
System.out.println("name " + new String(profilePics.getName())); | |
final byte[][] profilePicsValues = profilePics.getValues(); | |
for (final byte[] value : profilePicsValues) { | |
// as it doesn't contain real image bytes, we can just convert these | |
// bytes to string to verify | |
System.out.println("values " + new String(value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Developers Guide to get started