📄 mediaservice.java
字号:
((MediaEntry) entry).getMediaSource() : null; if (media == null) { return super.insert(feedUrl, entry); } InputStream resultStream = null; try { GDataRequest request; // Write as MIME multipart containing the entry and media MediaMultipart mediaMultipart = new MediaMultipart(entry, media); request = createRequest(GDataRequest.RequestType.INSERT, feedUrl, new ContentType(mediaMultipart.getContentType())); initMediaRequest(media, request); OutputStream outputStream = request.getRequestStream(); mediaMultipart.writeTo(outputStream); request.execute(); resultStream = request.getResponseStream(); return (E) parseEntry(entry.getClass(), resultStream); } catch (MessagingException e) { throw new ServiceException("Unable to write MIME multipart message", e); } finally { if (resultStream != null) { resultStream.close(); } } } /** * Inserts a new media resource read from {@link MediaSource} into a * media feed associated with the target service. It will return the * resulting entry that describes the inserted media, including * any additional attributes or extensions set by the GData server. * To insert both the entry and the media content in a single request, use * {@link #insert(URL, BaseEntry)}. * <p> * If the media source has a name ({@link MediaSource#getName()} that is * non-null), the name will be provided as a Slug header that is sent * along with request and <i>may</i> be used as a hint when determining * the ID, url, and/or title of the inserted resource. * * @param feedUrl the POST URI associated with the target feed. * @param entryClass the class used to parse the returned entry. * @param media the media source that contains the media content to insert. * @return the newly inserted entry returned by the service. * @throws IOException error communicating with the GData service. * @throws com.google.gdata.util.ParseException error parsing the returned * entry data. * @throws ServiceException insert request failed due to system error. * * @see com.google.gdata.data.BaseFeed#getEntryPostLink() * @see com.google.gdata.data.media.MediaFeed#insert(MediaSource) */ @SuppressWarnings({"unchecked"}) public <E extends BaseEntry> E insert(URL feedUrl, Class<E> entryClass, MediaSource media) throws IOException, ServiceException { if (media == null) { throw new NullPointerException("Must supply media source"); } InputStream resultStream = null; GDataRequest request; try { // Write media content only. request = createRequest(GDataRequest.RequestType.INSERT, feedUrl, new ContentType(media.getContentType())); initMediaRequest(media, request); // Write the media data MediaSource.Output.writeTo(media, request.getRequestStream()); request.execute(); resultStream = request.getResponseStream(); return parseEntry(entryClass, resultStream); } finally { if (resultStream != null) { resultStream.close(); } } } /** * Updates an existing entry and associated media resource by writing it * to the specified media edit URL. The resulting entry (after update) will * be returned. To update only the media content, use * {@link #updateMedia(URL, Class, MediaSource)}. * * @param mediaUrl the media edit URL associated with the resource. * @param entry the updated entry to be written to the server. * @return the updated entry returned by the service. * @throws IOException error communicating with the GData service. * @throws com.google.gdata.util.ParseException error parsing the updated * entry data. * @throws ServiceException update request failed due to system error. * * @see BaseEntry#getMediaEditLink() * @see MediaEntry#updateMedia(boolean) */ @SuppressWarnings({"unchecked"}) public <E extends BaseEntry> E updateMedia(URL mediaUrl, E entry) throws IOException, ServiceException { if (entry == null) { throw new NullPointerException("Must supply entry"); } // Since the input parameter is a media-edit URL, this method should // not be used to post Atom-only entries. These entries should be // sent to the edit URL. MediaSource media = (entry instanceof MediaEntry) ? ((MediaEntry) entry).getMediaSource() : null; if (media == null) { throw new NullPointerException("Must supply media source"); } InputStream resultStream = null; GDataRequest request; try { // Write as MIME multipart containing the entry and media MediaMultipart mediaMultipart = new MediaMultipart(entry, media); request = createRequest(GDataRequest.RequestType.UPDATE, mediaUrl, new ContentType(mediaMultipart.getContentType())); OutputStream outputStream = request.getRequestStream(); mediaMultipart.writeTo(outputStream); request.execute(); resultStream = request.getResponseStream(); return (E) parseEntry(entry.getClass(), resultStream); } catch (MessagingException e) { throw new ServiceException("Unable to write MIME multipart message", e); } finally { if (resultStream != null) { resultStream.close(); } } } /** * Updates an existing media resource with data read from the * {@link MediaSource} by writing it it to the specified media edit URL. * The resulting entry (after update) will be returned. To update both * the entry and the media content in a single request, use * {@link #updateMedia(URL, BaseEntry)}. * * @param mediaUrl the media edit URL associated with the resource. * @param entryClass the class that will be used to represent the * resulting entry. * @param media the media source data to be written to the server. * @return the updated Entry returned by the service. * @throws IOException error communicating with the GData service. * @throws com.google.gdata.util.ParseException error parsing the updated * entry data. * @throws ServiceException update request failed due to system error. * * @see BaseEntry#getMediaEditLink() * @see MediaEntry#updateMedia(boolean) */ @SuppressWarnings({"unchecked"}) public <E extends BaseEntry> E updateMedia(URL mediaUrl, Class<E> entryClass, MediaSource media) throws IOException, ServiceException { // Since the input parameter is a media-edit URL, this method should // not be used to post Atom-only entries. These entries should be // sent to the edit URL. if (media == null) { throw new NullPointerException("Must supply media source"); } InputStream resultStream = null; GDataRequest request; try { request = createRequest(GDataRequest.RequestType.UPDATE, mediaUrl, new ContentType(media.getContentType())); MediaSource.Output.writeTo(media, request.getRequestStream()); request.execute(); resultStream = request.getResponseStream(); return parseEntry(entryClass, resultStream); } finally { if (resultStream != null) { resultStream.close(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -