@Override
        public boolean next() throws IOException {
            try {
                if (underlying.hasNext()) {
                    underlying.loadNext();

                    Object baseObj = underlying.getBaseObject();
                    long recordOffset = underlying.getBaseOffset();
                    int recordLen = underlying.getRecordLength();

                    // Note that recordLen = keyLen + valueLen + 4 bytes (for the keyLen itself)
                    int keyLen = Platform.getInt(baseObj, recordOffset);
                    int valueLen = recordLen - keyLen - 4;
                    key.pointTo(baseObj, recordOffset + 4, keyLen);
                    value.pointTo(baseObj, recordOffset + 4 + keyLen, valueLen);

                    return true;
                } else {
                    key = null;
                    value = null;
                    cleanupResources();
                    return false;
                }
            } catch (IOException e) {
                cleanupResources();
                throw e;
            }
        }
Frees this sorter's in-memory data structures and cleans up its spill files.
indexr-query-opt/src/main/java/org/apache/spark/sql/execution/UnsafeKVExternalSorter.java