Skip to content

Commit

Permalink
CNSymbolPath: Add toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasemir committed Dec 15, 2016
1 parent ab2cdaa commit c27230d
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/etherip/types/CNSymbolPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,34 @@
*/
public class CNSymbolPath extends CNPath
{
class PathAndIndex{
class PathAndIndex
{
private String path;
private Integer index;
public PathAndIndex(String path, Integer index) {

public PathAndIndex(String path, Integer index)
{
this.path = path;
this.index = index;
}
public String getPath() {

public String getPath()
{
return path;
}
public Integer getIndex() {

public Integer getIndex()
{
return index;
}

@Override
public String toString()
{
if (index == null)
return path;
return path + "(" + index + ")";
}
};

private final Pattern PATTERN_BRACKETS = Pattern.compile("\\[(\\d+)\\]");
Expand Down Expand Up @@ -78,21 +92,22 @@ public void encode(final ByteBuffer buf, final StringBuilder log)
{
// spec 4 p.21: "ANSI extended symbol segment"
buf.put((byte) (getRequestSize() / 2));
for(PathAndIndex pi:paths){
for(PathAndIndex pi:paths)
{
String s = pi.getPath();
buf.put((byte) 0x91);
buf.put((byte) s.length());
buf.put(s.getBytes());
if (needPad(s))
buf.put((byte) 0);
Integer index = pi.getIndex();
if(index!=null){
//Path Segment 28, from wireshark
if (index!=null)
{
// Path Segment 28, from wireshark
buf.put((byte) 0x28);
buf.put(index.byteValue());
}
}

}

/** @return Is path of odd length, requiring a pad byte? */
Expand All @@ -101,4 +116,17 @@ private boolean needPad(String s)
// Findbugs: x%2==1 fails for negative numbers
return (s.length() % 2) != 0;
}

@Override
public String toString()
{
final StringBuilder buf = new StringBuilder();
for (PathAndIndex pi:paths)
{
if (buf.length() > 0)
buf.append(", ");
buf.append(pi);
}
return buf.toString();
}
}

0 comments on commit c27230d

Please sign in to comment.