| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.engine.impl; |
| 22 |
|
| 23 |
import org.apache.qpid.proton.amqp.Binary; |
| 24 |
|
| 25 |
public class StringUtils |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Converts the Binary to a quoted string. |
| 29 |
* |
| 30 |
* @param bin the Binary to convert |
| 31 |
* @param stringLength the maximum length of stringified content (excluding the quotes, and truncated indicator) |
| 32 |
* @param appendIfTruncated appends "...(truncated)" if not all of the payload is present in the string |
| 33 |
* @return the converted string |
| 34 |
*/ |
| 35 |
public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) |
| 36 |
{ |
| 37 |
if(bin == null) |
| 38 |
{ |
| 39 |
return "\"\""; |
| 40 |
} |
| 41 |
|
| 42 |
final byte[] binData = bin.getArray(); |
| 43 |
final int binLength = bin.getLength(); |
| 44 |
final int offset = bin.getArrayOffset(); |
| 45 |
|
| 46 |
StringBuilder str = new StringBuilder(); |
| 47 |
str.append("\""); |
| 48 |
|
| 49 |
int size = 0; |
| 50 |
boolean truncated = false; |
| 51 |
for (int i = 0; i < binLength; i++) |
| 52 |
{ |
| 53 |
byte c = binData[offset + i]; |
| 54 |
|
| 55 |
if (c > 31 && c < 127 && c != '\\') |
| 56 |
{ |
| 57 |
if (size + 1 <= stringLength) |
| 58 |
{ |
| 59 |
size += 1; |
| 60 |
str.append((char) c); |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
truncated = true; |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
if (size + 4 <= stringLength) |
| 71 |
{ |
| 72 |
size += 4; |
| 73 |
str.append(String.format("\\x%02x", c)); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
truncated = true; |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
str.append("\""); |
| 84 |
|
| 85 |
if (truncated && appendIfTruncated) |
| 86 |
{ |
| 87 |
str.append("...(truncated)"); |
| 88 |
} |
| 89 |
|
| 90 |
return str.toString(); |
| 91 |
} |
| 92 |
} |