| 203 | } |
| 204 | |
| 205 | private synchronized boolean doSend(String stat) { |
| 206 | try { |
| 207 | final byte[] data = stat.getBytes("utf-8"); |
| 208 | |
| 209 | // If we're going to go past the threshold of the buffer then flush. |
| 210 | // the +1 is for the potential '\n' in multi_metrics below |
| 211 | if(sendBuffer.remaining() < (data.length + 1)) { |
| 212 | flush(); |
| 213 | } |
| 214 | |
| 215 | if(sendBuffer.position() > 0) { // multiple metrics are separated by '\n' |
| 216 | sendBuffer.put( (byte) '\n'); |
| 217 | } |
| 218 | |
| 219 | sendBuffer.put(data); // append the data |
| 220 | |
| 221 | if(! multi_metrics) { |
| 222 | flush(); |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | |
| 227 | } catch (IOException e) { |
| 228 | log.error( |
| 229 | String.format("Could not send stat %s to host %s:%d", sendBuffer.toString(), _address.getHostName(), |
| 230 | _address.getPort()), e); |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public synchronized boolean flush() { |
| 236 | try { |