StringBuilder rtn = new StringBuilder();
int position = buffer.position();
buffer.position(offset);
+ size = Math.min(size, buffer.remaining());
for (int i = 0; i < size; i++) {
byte b = buffer.get();
import com.healthmarketscience.jackcess.impl.scsu.EndOfInputException;
import com.healthmarketscience.jackcess.impl.scsu.Expand;
import com.healthmarketscience.jackcess.impl.scsu.IllegalInputException;
+import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("\tName: (" + _table.getName() + ") " + _name);
+ ToStringBuilder sb = CustomToStringStyle.builder(this)
+ .append("name", "(" + _table.getName() + ") " + _name);
byte typeValue = _type.getValue();
if(_type.isUnsupported()) {
typeValue = getUnknownDataType();
}
- rtn.append("\n\tType: 0x" + Integer.toHexString(typeValue) +
- " (" + _type + ")");
- rtn.append("\n\tNumber: " + _columnNumber);
- rtn.append("\n\tLength: " + _columnLength);
- rtn.append("\n\tVariable length: " + _variableLength);
+ sb.append("type", "0x" + Integer.toHexString(typeValue) +
+ " (" + _type + ")")
+ .append("number", _columnNumber)
+ .append("length", _columnLength)
+ .append("variableLength", _variableLength);
if(_type.isTextual()) {
- rtn.append("\n\tCompressed Unicode: " + _textInfo._compressedUnicode);
- rtn.append("\n\tText Sort order: " + _textInfo._sortOrder);
+ sb.append("compressedUnicode", _textInfo._compressedUnicode)
+ .append("textSortOrder", _textInfo._sortOrder);
if(_textInfo._codePage > 0) {
- rtn.append("\n\tText Code Page: " + _textInfo._codePage);
+ sb.append("textCodePage", _textInfo._codePage);
}
if(isAppendOnly()) {
- rtn.append("\n\tAppend only: " + isAppendOnly());
+ sb.append("appendOnly", isAppendOnly());
}
if(isHyperlink()) {
- rtn.append("\n\tHyperlink: " + isHyperlink());
+ sb.append("hyperlink", isHyperlink());
}
}
if(_autoNumber) {
- rtn.append("\n\tLast AutoNumber: " + _autoNumberGenerator.getLast());
+ sb.append("lastAutoNumber", _autoNumberGenerator.getLast());
}
if(_complexInfo != null) {
- rtn.append("\n\tComplexInfo: " + _complexInfo);
+ sb.append("complexInfo", _complexInfo);
}
- rtn.append("\n\n");
- return rtn.toString();
+ return sb.toString();
}
/**
@Override
public String toString() {
- return "RawData: " + ByteUtil.toHexString(getBytes());
+ return CustomToStringStyle.valueBuilder(this)
+ .append(null, getBytes())
+ .toString();
}
private Object writeReplace() throws ObjectStreamException {
@Override
public String toString() {
- return _value + "(" + _version + ")";
+ return CustomToStringStyle.valueBuilder(this)
+ .append(null, _value + "(" + _version + ")")
+ .toString();
}
}
--- /dev/null
+/*
+Copyright (c) 2013 James Ahlborn
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
+*/
+
+package com.healthmarketscience.jackcess.impl;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.builder.StandardToStringStyle;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+/**
+ * Custom ToStringStyle for use with ToStringBuilder.
+ *
+ * @author James Ahlborn
+ */
+public class CustomToStringStyle extends StandardToStringStyle
+{
+ private static final long serialVersionUID = 0L;
+
+ private static final String FIELD_SEP = SystemUtils.LINE_SEPARATOR + " ";
+ private static final String IMPL_SUFFIX = "Impl";
+ private static final int MAX_BYTE_DETAIL_LEN = 20;
+
+ public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
+ private static final long serialVersionUID = 0L;
+ {
+ setContentStart("[");
+ setFieldSeparator(FIELD_SEP);
+ setFieldSeparatorAtStart(true);
+ setFieldNameValueSeparator(": ");
+ setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
+ setUseShortClassName(true);
+ }
+ };
+
+ public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
+ private static final long serialVersionUID = 0L;
+ {
+ setUseShortClassName(true);
+ setUseIdentityHashCode(false);
+ }
+ };
+
+ private CustomToStringStyle() {
+ }
+
+ public static ToStringBuilder builder(Object obj) {
+ return new ToStringBuilder(obj, INSTANCE);
+ }
+
+ public static ToStringBuilder valueBuilder(Object obj) {
+ return new ToStringBuilder(obj, VALUE_INSTANCE);
+ }
+
+ @Override
+ protected void appendClassName(StringBuffer buffer, Object obj) {
+ if(obj instanceof String) {
+ // the caller gave an "explicit" class name
+ buffer.append(obj);
+ } else {
+ super.appendClassName(buffer, obj);
+ }
+ }
+
+ @Override
+ protected String getShortClassName(Class clss) {
+ String shortName = super.getShortClassName(clss);
+ if(shortName.endsWith(IMPL_SUFFIX)) {
+ shortName = shortName.substring(0,
+ shortName.length() - IMPL_SUFFIX.length());
+ }
+ int idx = shortName.lastIndexOf('.');
+ if(idx >= 0) {
+ shortName = shortName.substring(idx + 1);
+ }
+ return shortName;
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Object value) {
+ if(value instanceof ByteBuffer) {
+ appendDetail(buffer, (ByteBuffer)value);
+ } else {
+ buffer.append(indent(value));
+ }
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Collection value) {
+ buffer.append("[");
+
+ // gather contents of list in a new StringBuffer
+ StringBuffer sb = new StringBuffer();
+ Iterator<?> iter = value.iterator();
+ if(iter.hasNext()) {
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+ appendInternal(sb, fieldName, iter.next(), true);
+ }
+ while(iter.hasNext()) {
+ sb.append(",");
+ appendFieldSeparator(sb);
+ appendInternal(sb, fieldName, iter.next(), true);
+ }
+
+ // indent entire list contents another level
+ buffer.append(indent(sb));
+
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(buffer);
+ }
+ buffer.append("]");
+ }
+
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Map value) {
+ buffer.append("{");
+
+ // gather contents of map in a new StringBuffer
+ StringBuffer sb = new StringBuffer();
+ @SuppressWarnings("unchecked")
+ Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
+ if(iter.hasNext()) {
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+ Map.Entry<?,?> e = iter.next();
+ sb.append(e.getKey()).append("=");
+ appendInternal(sb, fieldName, e.getValue(), true);
+ }
+ while(iter.hasNext()) {
+ sb.append(",");
+ appendFieldSeparator(sb);
+ Map.Entry<?,?> e = iter.next();
+ sb.append(e.getKey()).append("=");
+ appendInternal(sb, fieldName, e.getValue(), true);
+ }
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+
+ // indent entire map contents another level
+ buffer.append(indent(sb));
+
+ buffer.append("}");
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ byte[] array) {
+ appendDetail(buffer, PageChannel.wrap(array));
+ }
+
+ private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
+ int len = bb.remaining();
+ buffer.append("(").append(len).append(") ");
+ if(len > MAX_BYTE_DETAIL_LEN) {
+ buffer.append(ByteUtil.toHexString(bb, len, MAX_BYTE_DETAIL_LEN))
+ .append(" ...");
+ } else {
+ buffer.append(ByteUtil.toHexString(bb, len));
+ }
+ }
+
+ private static String indent(Object obj) {
+ return ((obj != null) ? obj.toString().replaceAll(
+ SystemUtils.LINE_SEPARATOR, FIELD_SEP) : null);
+ }
+}
import com.healthmarketscience.jackcess.IndexBuilder;
import static com.healthmarketscience.jackcess.impl.ByteUtil.ByteStream;
import static com.healthmarketscience.jackcess.impl.IndexCodes.*;
+import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("\n\tData number: ").append(_number);
- rtn.append("\n\tPage number: ").append(_rootPageNumber);
- rtn.append("\n\tIs Backing Primary Key: ").append(isBackingPrimaryKey());
- rtn.append("\n\tIs Unique: ").append(isUnique());
- rtn.append("\n\tIgnore Nulls: ").append(shouldIgnoreNulls());
- rtn.append("\n\tColumns: ").append(_columns);
- rtn.append("\n\tInitialized: ").append(_initialized);
+ ToStringBuilder sb = CustomToStringStyle.builder(this)
+ .append("dataNumber", _number)
+ .append("pageNumber", _rootPageNumber)
+ .append("isBackingPrimaryKey", isBackingPrimaryKey())
+ .append("isUnique", isUnique())
+ .append("ignoreNulls", shouldIgnoreNulls())
+ .append("columns", _columns)
+ .append("initialized", _initialized);
if(_initialized) {
try {
- rtn.append("\n\tEntryCount: ").append(getEntryCount());
+ sb.append("entryCount", getEntryCount());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
- rtn.append("\n").append(_pageCache.toString());
- return rtn.toString();
+ sb.append("pageCache", _pageCache);
+ return sb.toString();
}
/**
@Override
public String toString() {
- return "ColumnDescriptor " + getColumn() + "\nflags: " + getFlags();
+ return CustomToStringStyle.builder(this)
+ .append("column", getColumn())
+ .append("flags", getFlags())
+ .toString();
}
}
buffer.put((byte)getRowId().getRowNumber());
}
- protected final String entryBytesToString() {
- return (isValid() ? ", Bytes = " + ByteUtil.toHexString(
- ByteBuffer.wrap(_entryBytes), _entryBytes.length) :
- "");
+ protected final ToStringBuilder entryBytesToStringBuilder(
+ ToStringBuilder sb) {
+ if(isValid()) {
+ sb.append("bytes", _entryBytes);
+ }
+ return sb;
}
@Override
public String toString() {
- return "RowId = " + _rowId + entryBytesToString() + "\n";
+ return entryBytesToStringBuilder(
+ CustomToStringStyle.valueBuilder(this)
+ .append("rowId", _rowId))
+ .toString();
}
@Override
@Override
public String toString() {
- return ("Node RowId = " + getRowId() +
- ", SubPage = " + _subPageNumber + entryBytesToString() + "\n");
- }
-
+ return entryBytesToStringBuilder(
+ CustomToStringStyle.valueBuilder(this)
+ .append("rowId", getRowId())
+ .append("subPage", _subPageNumber))
+ .toString();
+ }
}
/**
@Override
public String toString() {
- return getClass().getSimpleName() + " CurPosition " + _curPos +
- ", PrevPosition " + _prevPos;
+ return CustomToStringStyle.valueBuilder(this)
+ .append("curPosition", _curPos)
+ .append("prevPosition", _prevPos)
+ .toString();
}
/**
@Override
public String toString() {
- return "Page = " + _dataPage.getPageNumber() + ", Idx = " + _idx +
- ", Entry = " + _entry + ", Between = " + _between;
+ return CustomToStringStyle.valueBuilder(this)
+ .append("page", _dataPage.getPageNumber())
+ .append("idx", _idx)
+ .append("entry", _entry)
+ .append("between", _between)
+ .toString();
}
}
@Override
public final String toString() {
List<Entry> entries = getEntries();
- return (isLeaf() ? "Leaf" : "Node") + "DataPage[" + getPageNumber() +
+
+ String objName =
+ (isLeaf() ? "Leaf" : "Node") + "DataPage[" + getPageNumber() +
"] " + getPrevPageNumber() + ", " + getNextPageNumber() + ", (" +
- getChildTailPageNumber() + "), " +
- ((isLeaf() && !entries.isEmpty()) ?
- ("[" + entries.get(0) + ", " +
- entries.get(entries.size() - 1) + "]") :
- entries);
+ getChildTailPageNumber() + ")";
+ ToStringBuilder sb = CustomToStringStyle.valueBuilder(objName);
+
+ if((isLeaf() && !entries.isEmpty())) {
+ sb.append("entryRange", "[" + entries.get(0) + ", " +
+ entries.get(entries.size() - 1) + "]");
+ } else {
+ sb.append("entries", entries);
+ }
+ return sb.toString();
}
}
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.Index;
import com.healthmarketscience.jackcess.IndexBuilder;
-import com.healthmarketscience.jackcess.RowId;
+import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("\tName: (").append(getTable().getName()).append(") ")
- .append(_name);
- rtn.append("\n\tNumber: ").append(_indexNumber);
- rtn.append("\n\tIs Primary Key: ").append(isPrimaryKey());
- rtn.append("\n\tIs Foreign Key: ").append(isForeignKey());
+ ToStringBuilder sb = CustomToStringStyle.builder(this)
+ .append("name", "(" + getTable().getName() + ") " + _name)
+ .append("number", _indexNumber)
+ .append("isPrimaryKey", isPrimaryKey())
+ .append("isForeignKey", isForeignKey());
if(_reference != null) {
- rtn.append("\n\tForeignKeyReference: ").append(_reference);
+ sb.append("foreignKeyReference", _reference);
}
- rtn.append(_data.toString());
- rtn.append("\n\n");
- return rtn.toString();
+ sb.append("data", _data);
+ return sb.toString();
}
public int compareTo(IndexImpl other) {
@Override
public String toString() {
- return new StringBuilder()
- .append("\n\t\tOther Index Number: ").append(_otherIndexNumber)
- .append("\n\t\tOther Table Page Num: ").append(_otherTablePageNumber)
- .append("\n\t\tIs Primary Table: ").append(isPrimaryTable())
- .append("\n\t\tIs Cascade Updates: ").append(isCascadeUpdates())
- .append("\n\t\tIs Cascade Deletes: ").append(isCascadeDeletes())
+ return CustomToStringStyle.builder(this)
+ .append("otherIndexNumber", _otherIndexNumber)
+ .append("otherTablePageNum", _otherTablePageNumber)
+ .append("isPrimaryTable", isPrimaryTable())
+ .append("isCascadeUpdates", isCascadeUpdates())
+ .append("isCascadeDeletes", isCascadeDeletes())
.toString();
}
}
import java.util.RandomAccess;
import static com.healthmarketscience.jackcess.impl.IndexData.*;
+import org.apache.commons.lang.builder.ToStringBuilder;
/**
* Manager of the index pages for a IndexData.
}
/**
- * Dumps the given index page to a StringBuilder
+ * Collects all the cache pages in the cache.
*
- * @param rtn the StringBuilder to update
- * @param dpMain the index page to dump
+ * @param paages the List to update
+ * @param dpMain the index page to collect
*/
- private void dumpPage(StringBuilder rtn, DataPageMain dpMain) {
+ private List<Object> collectPages(List<Object> pages, DataPageMain dpMain) {
try {
CacheDataPage cacheDataPage = new CacheDataPage(dpMain);
- rtn.append(cacheDataPage).append("\n");
+ pages.add(cacheDataPage);
if(!dpMain._leaf) {
for(Entry e : cacheDataPage._extra._entryView) {
DataPageMain childMain = dpMain.getChildPage(e);
- dumpPage(rtn, childMain);
+ collectPages(pages, childMain);
}
}
} catch(IOException e) {
- rtn.append("Page[" + dpMain._pageNumber + "]: " + e);
+ pages.add("DataPage[" + dpMain._pageNumber + "]: " + e);
}
+ return pages;
}
/**
@Override
public String toString() {
+ ToStringBuilder sb = CustomToStringStyle.builder(this);
if(_rootPage == null) {
- return "Cache: (uninitialized)";
+ sb.append("pages", "(uninitialized)");
+ } else {
+ sb.append("pages", collectPages(new ArrayList<Object>(), _rootPage));
}
-
- StringBuilder rtn = new StringBuilder("Cache: \n");
- dumpPage(rtn, _rootPage);
- return rtn.toString();
+ return sb.toString();
}
/**
@Override
public String toString() {
- return "DPExtra: " + _entryView;
- }
+ return CustomToStringStyle.builder("DPExtra")
+ .append(null, _entryView)
+ .toString();
+ }
}
/**
@Override
public String toString() {
- StringBuilder sb = new StringBuilder();
- for(Iterator<PropertyMapImpl> iter = iterator(); iter.hasNext(); ) {
- sb.append(iter.next());
- if(iter.hasNext()) {
- sb.append("\n");
- }
- }
- return sb.toString();
+ return CustomToStringStyle.builder(this)
+ .append(null, _maps.values())
+ .toString();
}
/**
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("\tName: " + _name);
- rtn.append("\n\tFromTable: " + _fromTable.getName());
- rtn.append("\n\tFromColumns: " + _fromColumns);
- rtn.append("\n\tToTable: " + _toTable.getName());
- rtn.append("\n\tToColumns: " + _toColumns);
- rtn.append("\n\tFlags: " + Integer.toHexString(_flags));
- rtn.append("\n\n");
- return rtn.toString();
+ return CustomToStringStyle.builder(this)
+ .append("name", _name)
+ .append("fromTable", _fromTable.getName())
+ .append("fromColumns", _fromColumns)
+ .append("toTable", _toTable.getName())
+ .append("toColumns", _toColumns)
+ .append("flags", Integer.toHexString(_flags))
+ .toString();
}
}
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("Type: " + _tableType +
- ((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)"));
- rtn.append("\nName: " + _name);
- rtn.append("\nRow count: " + _rowCount);
- rtn.append("\nColumn count: " + _columns.size());
- rtn.append("\nIndex (data) count: " + _indexCount);
- rtn.append("\nLogical Index count: " + _logicalIndexCount);
- rtn.append("\nColumns:\n");
- for(ColumnImpl col : _columns) {
- rtn.append(col);
- }
- rtn.append("\nIndexes:\n");
- for(Index index : _indexes) {
- rtn.append(index);
- }
- rtn.append("\nOwned pages: " + _ownedPages + "\n");
- return rtn.toString();
+ return CustomToStringStyle.builder(this)
+ .append("type", (_tableType +
+ ((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)")))
+ .append("name", _name)
+ .append("rowCount", _rowCount)
+ .append("columnCount", _columns.size())
+ .append("indexCount(data)", _indexCount)
+ .append("logicalIndexCount", _logicalIndexCount)
+ .append("columns", _columns)
+ .append("indexes", _indexes)
+ .append("ownedPages", _ownedPages)
+ .toString();
}
/**
}
@Override
- public String toString()
- {
- return "RowState: headerRowId = " + _headerRowId + ", finalRowId = " +
- _finalRowId;
+ public String toString() {
+ return CustomToStringStyle.valueBuilder(this)
+ .append("headerRowId", _headerRowId)
+ .append("finalRowId", _finalRowId)
+ .toString();
}
}
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
import java.util.BitSet;
+import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import com.healthmarketscience.jackcess.RowId;
/**
* Describes which database pages a particular table uses
@Override
public String toString() {
- StringBuilder builder = new StringBuilder(
- "(" + _handler.getClass().getSimpleName() +
- ") page numbers (range " + _startPage + " " + _endPage + "): [");
+ List<String> ranges = new ArrayList<String>();
PageCursor pCursor = cursor();
int curRangeStart = Integer.MIN_VALUE;
int prevPage = Integer.MIN_VALUE;
if(nextPage != (prevPage + 1)) {
if(prevPage >= 0) {
- rangeToString(builder, curRangeStart, prevPage);
+ rangeToString(ranges, curRangeStart, prevPage);
}
curRangeStart = nextPage;
}
prevPage = nextPage;
}
if(prevPage >= 0) {
- rangeToString(builder, curRangeStart, prevPage);
+ rangeToString(ranges, curRangeStart, prevPage);
}
- builder.append("]");
- return builder.toString();
+ return CustomToStringStyle.valueBuilder(
+ _handler.getClass().getSimpleName())
+ .append("range", "(" + _startPage + "-" + _endPage + ")")
+ .append("pageNumbers", ranges)
+ .toString();
}
- private static void rangeToString(StringBuilder builder, int rangeStart,
+ private static void rangeToString(List<String> ranges, int rangeStart,
int rangeEnd)
{
- builder.append(rangeStart);
if(rangeEnd > rangeStart) {
- builder.append("-").append(rangeEnd);
+ ranges.add(rangeStart + "-" + rangeEnd);
+ } else {
+ ranges.add(String.valueOf(rangeStart));
}
- builder.append(", ");
}
private abstract class Handler
import com.healthmarketscience.jackcess.complex.ComplexValue;
import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
import com.healthmarketscience.jackcess.impl.ColumnImpl;
+import com.healthmarketscience.jackcess.impl.CustomToStringStyle;
import com.healthmarketscience.jackcess.impl.TableImpl;
/**
@Override
public String toString() {
- StringBuilder rtn = new StringBuilder();
- rtn.append("\n\t\tComplexType: " + getType());
- rtn.append("\n\t\tComplexTypeId: " + _complexTypeId);
- return rtn.toString();
+ return CustomToStringStyle.valueBuilder(this)
+ .append("complexType", getType())
+ .append("complexTypeId", _complexTypeId)
+ .toString();
}
protected static void diffFlatColumns(Table typeObjTable,
int chunkIndex = getChunkIndex(position);
int chunkOffset = getChunkOffset(position);
- long numBytes = 0;
+ long numBytes = 0L;
while(count > 0L) {
int chunkBytes = (int)Math.min(count, CHUNK_SIZE - chunkOffset);