// Path: c5-replicator-util/src/main/java/c5db/log/EntryEncodingUtil.java
// public static <T> T decodeAndCheckCrc(InputStream inputStream, Schema<T> schema)
//     throws IOException, CrcError {
//   // TODO this should check the length first and compare it with a passed-in maximum length
//   final T message = schema.newMessage();
//   final CrcInputStream crcStream = new CrcInputStream(inputStream, new Adler32());
//   ProtobufIOUtil.mergeDelimitedFrom(crcStream, message, schema);
// 
//   final long computedCrc = crcStream.getValue();
//   final long diskCrc = readCrc(inputStream);
//   if (diskCrc != computedCrc) {
//     throw new CrcError("CRC mismatch on deserialized message " + message.toString());
//   }
// 
//   return message;
// }
// 
// Path: c5-replicator-util/src/main/java/c5db/log/EntryEncodingUtil.java
// public static <T> List<ByteBuffer> encodeWithLengthAndCrc(Schema<T> schema, T message) {
//   final LinkBuffer messageBuf = new LinkBuffer();
//   final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(messageBuf);
// 
//   try {
//     schema.writeTo(lcpo, message);
// 
//     final int length = Ints.checkedCast(lcpo.buffer.size());
//     final LinkBuffer lengthBuf = new LinkBuffer().writeVarInt32(length);
// 
//     return appendCrcToBufferList(
//         Lists.newArrayList(
//             Iterables.concat(lengthBuf.finish(), messageBuf.finish()))
//     );
//   } catch (IOException e) {
//     // This method performs no IO, so it should not actually be possible for an IOException to be thrown.
//     // But just in case...
//     throw new RuntimeException(e);
//   }
// }


import c5db.log.generated.OLogContentType;
import c5db.log.generated.OLogEntryHeader;
import io.protostuff.Schema;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.List;
import static c5db.log.EntryEncodingUtil.decodeAndCheckCrc;
import static c5db.log.EntryEncodingUtil.encodeWithLengthAndCrc;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/*
 * Copyright 2014 WANdisco
 *
 *  WANdisco licenses this file to you under the Apache License,
 *  version 2.0 (the "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *  License for the specific language governing permissions and limitations
 *  under the License.
 */

package c5db.log;




public class EntryEncodingUtilTest {
  private static final Schema<OLogEntryHeader> SCHEMA = OLogEntryHeader.getSchema();
  private static final OLogEntryHeader TEST_ENTRY =
// Path: c5-replicator-util/src/main/java/c5db/log/EntryEncodingUtil.java
// public static <T> T decodeAndCheckCrc(InputStream inputStream, Schema<T> schema)
//     throws IOException, CrcError {
//   // TODO this should check the length first and compare it with a passed-in maximum length
//   final T message = schema.newMessage();
//   final CrcInputStream crcStream = new CrcInputStream(inputStream, new Adler32());
//   ProtobufIOUtil.mergeDelimitedFrom(crcStream, message, schema);
// 
//   final long computedCrc = crcStream.getValue();
//   final long diskCrc = readCrc(inputStream);
//   if (diskCrc != computedCrc) {
//     throw new CrcError("CRC mismatch on deserialized message " + message.toString());
//   }
// 
//   return message;
// }
// 
// Path: c5-replicator-util/src/main/java/c5db/log/EntryEncodingUtil.java
// public static <T> List<ByteBuffer> encodeWithLengthAndCrc(Schema<T> schema, T message) {
//   final LinkBuffer messageBuf = new LinkBuffer();
//   final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(messageBuf);
// 
//   try {
//     schema.writeTo(lcpo, message);
// 
//     final int length = Ints.checkedCast(lcpo.buffer.size());
//     final LinkBuffer lengthBuf = new LinkBuffer().writeVarInt32(length);
// 
//     return appendCrcToBufferList(
//         Lists.newArrayList(
//             Iterables.concat(lengthBuf.finish(), messageBuf.finish()))
//     );
//   } catch (IOException e) {
//     // This method performs no IO, so it should not actually be possible for an IOException to be thrown.
//     // But just in case...
//     throw new RuntimeException(e);
//   }
// }


// Path: c5-replicator-log/src/test/java/c5db/log/EntryEncodingUtilTest.java
import c5db.log.generated.OLogContentType;
import c5db.log.generated.OLogEntryHeader;
import io.protostuff.Schema;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.List;
import static c5db.log.EntryEncodingUtil.decodeAndCheckCrc;
import static c5db.log.EntryEncodingUtil.encodeWithLengthAndCrc;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/*
 * Copyright 2014 WANdisco
 *
 *  WANdisco licenses this file to you under the Apache License,
 *  version 2.0 (the "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *  License for the specific language governing permissions and limitations
 *  under the License.
 */

package c5db.log;




public class EntryEncodingUtilTest {
  private static final Schema<OLogEntryHeader> SCHEMA = OLogEntryHeader.getSchema();
  private static final OLogEntryHeader TEST_ENTRY =
      new OLogEntryHeader(Long.MAX_VALUE, Long.MAX_VALUE, Integer.MAX_VALUE, OLogContentType.DATA);