Java tutorial
/* * Copyright Tek Counsel LLC 2013 * * Licensed 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 com.tc.simple.apn.factories; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.codec.binary.Hex; import org.apache.commons.io.IOUtils; import com.tc.simple.apn.Feedback; public class FeedbackFactory implements IFeedbackFactory { private static final Logger logger = Logger.getLogger(FeedbackFactory.class.getName()); public List<Feedback> createFeedback(InputStream in) { List<Feedback> list = new ArrayList<Feedback>(); try { byte[] byteMe = IOUtils.toByteArray(in); int startRange = 0; int endRange = 38; //38 byte chunks per feedback item. while (startRange < byteMe.length) { //init the value object to hold the feedback data. Feedback feedback = new Feedback(); //build the item based on range byte[] item = Arrays.copyOfRange(byteMe, startRange, endRange);//38 byte chunks. byte[] date = Arrays.copyOfRange(item, 0, 4); byte[] size = Arrays.copyOfRange(item, 4, 6); byte[] token = Arrays.copyOfRange(item, 6, item.length); ByteBuffer dateWrap = ByteBuffer.wrap(date); ByteBuffer javaSize = ByteBuffer.wrap(size); //set the date (returns number of seconds from unix epoch date) feedback.setDate(new Date(dateWrap.getInt() * 1000L)); //get the size of the token (should always be 32) feedback.setSize(javaSize.getShort()); //drop in our encoded token (will be used as our key for marking the user's token doc as failed) feedback.setToken(String.valueOf(Hex.encodeHex(token))); //add it to our collection list.add(feedback); //increment the start range startRange = startRange + 38; //increment the end range. endRange = endRange + 38; } } catch (Exception e) { logger.log(Level.SEVERE, null, e); } return list; } }