List of usage examples for java.io ByteArrayInputStream reset
public synchronized void reset()
From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper.DefaultHtmlContentGenerator.java
private String queryMimeType(final byte[] data) throws IOException { final ByteArrayInputStream stream = new ByteArrayInputStream(data); if (isGIF(stream)) { return "image/gif"; }//from w w w.j a v a 2 s .c om stream.reset(); if (isJPEG(stream)) { return "image/jpeg"; } stream.reset(); if (isPNG(stream)) { return "image/png"; } return null; }
From source file:org.sensorhub.test.service.sos.TestSOSService.java
@Test(expected = OGCException.class) public void testGetResultWrongOffering() throws Exception { deployService(buildSensorProvider1(), buildSensorProvider2()); InputStream is = new URL(SERVICE_ENDPOINT + "?service=SOS&version=2.0&request=GetResult&offering=urn:mysos:wrong&observedProperty=urn:blabla:temperature") .openStream();//from w ww . j a v a 2s . co m ByteArrayOutputStream os = new ByteArrayOutputStream(); IOUtils.copy(is, os); // read back and print ByteArrayInputStream bis = new ByteArrayInputStream(os.toByteArray()); IOUtils.copy(bis, System.out); bis.reset(); // parse and generate exception OGCExceptionReader.parseException(bis); }
From source file:org.sensorhub.test.service.sos.TestSOSService.java
@Test(expected = OGCException.class) public void testGetObsWrongFormat() throws Exception { deployService(buildSensorProvider1()); InputStream is = new URL(SERVICE_ENDPOINT + "?service=SOS&version=2.0&request=GetObservation&offering=urn:mysos:sensor1&observedProperty=urn:blabla:temperature&responseFormat=badformat") .openStream();//w w w . ja va 2 s . c o m ByteArrayOutputStream os = new ByteArrayOutputStream(); IOUtils.copy(is, os); // read back and print ByteArrayInputStream bis = new ByteArrayInputStream(os.toByteArray()); IOUtils.copy(bis, System.out); bis.reset(); // parse and generate exception OGCExceptionReader.parseException(bis); }
From source file:org.wso2.carbon.analytics.datasource.core.AnalyticsFileSystemTest.java
private void fileReadSeekPosition(String path, int n, int chunk, int... locs) throws IOException { byte[] data = generateData(n); OutputStream out = this.analyticsFileSystem.createOutput(path); out.write(data, 0, data.length);/* w w w . j a va 2 s. c om*/ out.close(); byte[] din = new byte[chunk]; ByteArrayInputStream bin = new ByteArrayInputStream(data); byte[] din2 = new byte[chunk]; DataInput in = this.analyticsFileSystem.createInput(path); int count, count2; for (int i : locs) { in.seek(i); Assert.assertEquals(in.getPosition(), i); count = in.read(din, 0, din.length); Assert.assertEquals(in.getPosition(), i + (count < 0 ? 0 : count)); bin.reset(); bin.skip(i); count2 = bin.read(din2, 0, din2.length); Assert.assertEquals(count, count2); Assert.assertEquals(din, din2); } }
From source file:org.xwiki.contrib.ldap.XWikiLDAPUtils.java
/** * Sync user avatar with LDAP/*from w ww . ja va 2 s . co m*/ * * @param ldapUid value of the unique identifier for the user to update. * @param userProfile the XWiki user profile document. * @param context the XWiki context. * @return true if avatar was updated, false otherwise. * @throws XWikiException */ protected boolean updatePhotoFromLdap(String ldapUid, XWikiDocument userProfile, XWikiContext context) throws XWikiException { BaseClass userClass = context.getWiki().getUserClass(context); BaseObject userObj = userProfile.getXObject(userClass.getDocumentReference()); // Get current user avatar String userAvatar = userObj.getStringValue("avatar"); XWikiAttachment currentPhoto = null; if (userAvatar != null) { currentPhoto = userProfile.getAttachment(userAvatar); } // Get properties String photoAttachmentName = this.configuration .getLDAPParam(XWikiLDAPConfig.PREF_LDAP_PHOTO_ATTACHMENT_NAME, "ldapPhoto", context); String ldapPhotoAttribute = this.configuration.getLDAPParam(XWikiLDAPConfig.PREF_LDAP_PHOTO_ATTRIBUTE, XWikiLDAPConfig.DEFAULT_PHOTO_ATTRIBUTE, context); // Proceed only if any of conditions are true: // 1. User do not have avatar currently // 2. User have avatar and avatar file name is equals to PREF_LDAP_PHOTO_ATTACHMENT_NAME if (StringUtils.isEmpty(userAvatar) || photoAttachmentName.equals(FilenameUtils.getBaseName(userAvatar)) || currentPhoto == null) { // Obtain photo from LDAP byte[] ldapPhoto = null; List<XWikiLDAPSearchAttribute> ldapAttributes = searchUserAttributesByUid(ldapUid, new String[] { ldapPhotoAttribute }); if (ldapAttributes != null) { // searchUserAttributesByUid method may return dn as 1st element // Let's iterate over array and search ldapPhotoAttribute for (XWikiLDAPSearchAttribute attribute : ldapAttributes) { if (attribute.name.equals(ldapPhotoAttribute)) { ldapPhoto = attribute.byteValue; } } } if (ldapPhoto != null) { ByteArrayInputStream ldapPhotoInputStream = new ByteArrayInputStream(ldapPhoto); // Try to guess image type String ldapPhotoType = guessImageType(ldapPhotoInputStream); ldapPhotoInputStream.reset(); if (ldapPhotoType != null) { String photoAttachmentFullName = photoAttachmentName + "." + ldapPhotoType.toLowerCase(); if (!StringUtils.isEmpty(userAvatar) && currentPhoto != null) { try { // Compare current xwiki avatar and LDAP photo if (!IOUtils.contentEquals(currentPhoto.getContentInputStream(context), ldapPhotoInputStream)) { ldapPhotoInputStream.reset(); // Store photo return addPhotoToProfile(userProfile, context, ldapPhotoInputStream, ldapPhoto.length, photoAttachmentFullName); } } catch (IOException ex) { LOGGER.error(ex.getMessage()); } } else if (addPhotoToProfile(userProfile, context, ldapPhotoInputStream, ldapPhoto.length, photoAttachmentFullName)) { PropertyClass avatarProperty = (PropertyClass) userClass.getField("avatar"); userObj.safeput("avatar", avatarProperty.fromString(photoAttachmentFullName)); return true; } } else { LOGGER.info("Unable to determine LDAP photo image type."); } } else if (currentPhoto != null) { // Remove current avatar PropertyClass avatarProperty = (PropertyClass) userClass.getField("avatar"); userObj.safeput("avatar", avatarProperty.fromString("")); return true; } } return false; }
From source file:ws.michalski.sepa.pain.SEPAAutoReader.java
/** * Liefert xmlns Attribut von des XML /*from ww w . ja v a2s . co m*/ * @param String zum Suchen als XPath Ausdruck * @param Attribut * @return String */ private String getXmlnsAttribut(ByteArrayInputStream is) { String erg = null; is.reset(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; Document doc = null; try { db = dbf.newDocumentBuilder(); doc = db.parse(is); } catch (ParserConfigurationException e) { log.error(e); } catch (SAXException e) { log.error(e); } catch (IOException e) { log.error(e); } Element rootNode = doc.getDocumentElement(); erg = rootNode.getAttribute("xmlns"); is.reset(); return erg; }