diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5df81271..57ee2ec1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
 # Splunk SDK for Java Changelog
 
+## Version 1.6.5
+
+### Bug Fixes
+
+* Fixed bug for push back buffer is full when exporting data in XML (GitHub PR [#125](https://github.com/splunk/splunk-sdk-java/pull/125)).
+
 ## Version 1.6.4
 
 ### Bug Fixes
diff --git a/README.md b/README.md
index 19f64794..914502b4 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
 [![Build Status](https://travis-ci.org/splunk/splunk-sdk-java.svg?branch=master)](https://travis-ci.org/splunk/splunk-sdk-java)
 # The Splunk Software Development Kit for Java
 
-#### Version 1.6.4
+#### Version 1.6.5
 
 The Splunk Software Development Kit (SDK) for Java contains library code and
 examples designed to enable developers to build applications using Splunk.
diff --git a/build.xml b/build.xml
index 401628bf..5a96829d 100644
--- a/build.xml
+++ b/build.xml
@@ -18,7 +18,7 @@
 
 <project name="splunk-sdk-java" basedir="." default="dist" xmlns:jacoco="antlib:org.jacoco.ant"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="antlib:org.jacoco.ant ">
-    <property name="version.number" value="1.6.4"/>
+    <property name="version.number" value="1.6.5"/>
 
     <!-- Paths within the repository that are referenced
          multiple times. -->
diff --git a/deploy b/deploy
index 51348fe1..bf56c132 100755
--- a/deploy
+++ b/deploy
@@ -2,7 +2,7 @@
 
 declare -r scriptDirectory="$(dirname $(readlink -e $0))"
 declare -r scriptName="$(basename $0)"
-declare -r version="1.6.4"
+declare -r version="1.6.5"
 
 if [[ $# -ne 1 ]]; then
     echo 1>&2 "Usage: ${scriptName} {local|staging||production}"
diff --git a/deploy.md b/deploy.md
index 3ca67547..253bf780 100644
--- a/deploy.md
+++ b/deploy.md
@@ -9,8 +9,8 @@ deploy \<repository-name>
 
 ##DESCRIPTION
 
-Deploy transmits **dist/splunk-1.6.4.jar**, **dist/splunk-1.6.4-javadoc.jar**, and 
-**dist/splunk-1.6.4-sources.jar** to the **local**, **staging**, or **production**
+Deploy transmits **dist/splunk-1.6.5.jar**, **dist/splunk-1.6.5-javadoc.jar**, and 
+**dist/splunk-1.6.5-sources.jar** to the **local**, **staging**, or **production**
 maven repository. Repository names are mapped to locations as follows.
 
 | repository-name | location                                                       |
@@ -21,18 +21,18 @@ maven repository. Repository names are mapped to locations as follows.
 
 After deployment you should find this tree structure at the location of your repository
 
-    com/splunk/splunk/1.6.4/
-    ├── splunk-1.6.4-javadoc.jar
-    ├── splunk-1.6.4-javadoc.jar.md5
-    ├── splunk-1.6.4-javadoc.jar.sha1
-    ├── splunk-1.6.4-sources.jar
-    ├── splunk-1.6.4-sources.jar.md5
-    ├── splunk-1.6.4-sources.jar.sha1
-    ├── splunk-1.6.4.jar
-    ├── splunk-1.6.4.jar.md5
-    ├── splunk-1.6.4.jar.sha1
-    ├── splunk-1.6.4.pom
-    ├── splunk-1.6.4.pom.md5
-    └── splunk-1.6.4.pom.sha1
+    com/splunk/splunk/1.6.5/
+    ├── splunk-1.6.5-javadoc.jar
+    ├── splunk-1.6.5-javadoc.jar.md5
+    ├── splunk-1.6.5-javadoc.jar.sha1
+    ├── splunk-1.6.5-sources.jar
+    ├── splunk-1.6.5-sources.jar.md5
+    ├── splunk-1.6.5-sources.jar.sha1
+    ├── splunk-1.6.5.jar
+    ├── splunk-1.6.5.jar.md5
+    ├── splunk-1.6.5.jar.sha1
+    ├── splunk-1.6.5.pom
+    ├── splunk-1.6.5.pom.md5
+    └── splunk-1.6.5.pom.sha1
 
 Verify this structure prior to release.
diff --git a/splunk/com/splunk/HttpService.java b/splunk/com/splunk/HttpService.java
index 1e66485b..a66cb5f2 100644
--- a/splunk/com/splunk/HttpService.java
+++ b/splunk/com/splunk/HttpService.java
@@ -73,7 +73,7 @@ public boolean verify(String s, SSLSession sslSession) {
     private String prefix = null;
 
     static Map<String, String> defaultHeader = new HashMap<String, String>() {{
-        put("User-Agent", "splunk-sdk-java/1.6.4");
+        put("User-Agent", "splunk-sdk-java/1.6.5");
         put("Accept", "*/*");
     }};
 
diff --git a/splunk/com/splunk/InsertRootElementFilterInputStream.java b/splunk/com/splunk/InsertRootElementFilterInputStream.java
index 3d4da339..b668f850 100644
--- a/splunk/com/splunk/InsertRootElementFilterInputStream.java
+++ b/splunk/com/splunk/InsertRootElementFilterInputStream.java
@@ -16,9 +16,6 @@
 package com.splunk;
 
 import java.io.*;
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-import java.util.concurrent.Callable;
 
 /**
  * Takes an InputStream containing a UTF-8 encoded XML document containing one or more
@@ -31,22 +28,35 @@
  * it is filtering.
  */
 class InsertRootElementFilterInputStream extends FilterInputStream {
+    private static final int REREAD_BUFFER_SIZE = 512;
+    private static byte[] resultsTagBytes;
     private final ByteArrayInputStream suffix = new ByteArrayInputStream("</doc>".getBytes("UTF-8"));
+    private ByteArrayInputStream beforeResultsBuffer;
     private boolean wrotePrefix;
 
     private byte[] oneByte = new byte[1];
 
+    static {
+        try {
+            resultsTagBytes = "results".getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            //should not be thrown because UTF-8 is supported
+            throw new RuntimeException(e);
+        }
+    }
+
     InsertRootElementFilterInputStream(InputStream in) throws IOException {
         // Wrap in with a pushback stream so we can write our modified version back
         // onto the beginning of it.
-        super(new PushbackInputStream(in, 512));
+        super(new PushbackInputStream(in, REREAD_BUFFER_SIZE));
+
         PushbackInputStream pin = (PushbackInputStream)this.in;
 
         // Read bytes until we reach '>', then push everything we read, followed by "<doc>",
         // back onto the stream. If we run out of input before we reach '>', then don't
         // modify the stream.
         ByteArrayOutputStream beforeResultsChars = new ByteArrayOutputStream();
-        ByteArrayOutputStream atResultsChars = new ByteArrayOutputStream();
+        beforeResultsBuffer = new ByteArrayInputStream(new byte[0]);
 
         int ch;
         while (true) {
@@ -57,32 +67,19 @@ class InsertRootElementFilterInputStream extends FilterInputStream {
                 pin.unread(beforeResultsChars.toByteArray());
                 return;
             } else if (ch == (int)'<') {
-                // Try extending
-                atResultsChars.reset();
-                int ech;
-                boolean matched = true;
-                for (byte b : "results".getBytes("UTF-8")) {
-                    ech = this.in.read();
-                    atResultsChars.write(ech);
-                    if (ech != b) {
-                        // Extension failed. Put the bytes back on and search again.
-                        pin.unread(atResultsChars.toByteArray());
-                        matched = false;
-                        break;
-                    }
-                }
+                boolean resultsTag = isResultsTag(pin);
 
-                if (matched) {
+                if (resultsTag) {
                     // If we reach here, the extension succeeded, so we insert <doc>, unread everything,
                     // and return.
 
                     // Unread the match.
-                    pin.unread(atResultsChars.toByteArray());
+                    pin.unread(InsertRootElementFilterInputStream.resultsTagBytes);
                     // Unread the opening '<' that led to our extension
                     pin.unread(ch);
-                    // Add a '<doc>' element to our read charactes and unread them.
+                    // Add a '<doc>' element to our read characters
                     beforeResultsChars.write("<doc>".getBytes("UTF-8"));
-                    pin.unread(beforeResultsChars.toByteArray());
+                    beforeResultsBuffer = new ByteArrayInputStream(beforeResultsChars.toByteArray());
                     wrotePrefix = true;
                     return;
                 } else {
@@ -96,9 +93,38 @@ class InsertRootElementFilterInputStream extends FilterInputStream {
         }
     }
 
+    private boolean isResultsTag(PushbackInputStream pin) throws IOException {
+        // Try extending
+        ByteArrayOutputStream atResultsChars = new ByteArrayOutputStream();
+        int ech;
+        boolean resultsTag = true;
+        for (byte b : resultsTagBytes) {
+            ech = this.in.read();
+            atResultsChars.write(ech);
+            if (ech != b) {
+                // Extension failed. Put the bytes back on and search again.
+                pin.unread(atResultsChars.toByteArray());
+                resultsTag = false;
+                break;
+            }
+        }
+        return resultsTag;
+    }
+
     @Override
     public int read(byte[] buffer, int offset, int length) throws IOException {
-        int result = in.read(buffer, offset, length);
+        // first we read from the buffer before the first results xml tag
+        int result = 0;
+        int availableFromBuffer = beforeResultsBuffer.available();
+        if (offset < availableFromBuffer) {
+            result = beforeResultsBuffer.read(buffer, offset, length);
+            if (length <= result) {
+                return result;
+            }
+        }
+
+        // then we read from the original input stream
+        result += in.read(buffer, offset+result, length-result);
         if (result == -1 && wrotePrefix) {
             // No more bytes to read from in, and we have written '<doc>' earlier in the stream
             return suffix.read(buffer, offset, length);
diff --git a/tests/com/splunk/InsertRootElementFilterInputStreamTest.java b/tests/com/splunk/InsertRootElementFilterInputStreamTest.java
index 868a911d..b6bc37ab 100644
--- a/tests/com/splunk/InsertRootElementFilterInputStreamTest.java
+++ b/tests/com/splunk/InsertRootElementFilterInputStreamTest.java
@@ -48,12 +48,190 @@ public void testNoDtd() throws IOException {
     }
 
     @Test
-    public void testNoResultsElement() throws IOException {
-        InputStream stream = new ByteArrayInputStream("boris the mad baboon".getBytes("UTF-8"));
+    public void testNoXml() throws IOException {
+        String input = "boris the mad baboon";
+        InputStream stream = new ByteArrayInputStream(input.getBytes("UTF-8"));
         InputStream filteredStream = new InsertRootElementFilterInputStream(stream);
 
         String found = SDKTestCase.streamToString(filteredStream);
-        String expected = "boris the mad baboon";
+
+        Assert.assertEquals(input, found);
+    }
+
+    @Test
+    public void testNoResults() throws IOException {
+        String input = "<?xml encoding=\"UTF-8\"><name/><firstName/>";
+        InputStream stream = new ByteArrayInputStream(input.getBytes("UTF-8"));
+        InputStream filteredStream = new InsertRootElementFilterInputStream(stream);
+
+        String found = SDKTestCase.streamToString(filteredStream);
+
+        Assert.assertEquals(input, found);
+    }
+
+    @Test
+    public void testBigPreambleBeforeResults() throws IOException {
+        InputStream stream = new ByteArrayInputStream(("<?xml encoding=\"UTF-8\">" +
+                "<!-- A comment big enough to break things, Splunkd sometimes writes a lot of spaces before starting the real payload. -->" +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "<results/><results/>").getBytes("UTF-8"));
+        InputStream filteredStream = new InsertRootElementFilterInputStream(stream);
+
+        String found = SDKTestCase.streamToString(filteredStream);
+        String expected = "<?xml encoding=\"UTF-8\">" +
+                "<!-- A comment big enough to break things, Splunkd sometimes writes a lot of spaces before starting the real payload. -->" +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "                                                                                                                         " +
+                "<doc><results/><results/></doc>";
 
         Assert.assertEquals(expected, found);
     }
